I have to upload photo to a folder in a HTTP server from a window application using web-service.I have converted the photo into stream and passed to a web-service but how can i save it to a location (eg: http:/siteaddress/images/photos/) in the server.
the web service has to get the stream from the client and convert it back to file in the location you mentioned.
string destinationFolder = Server.MapPath("/images/photos");
then assuming that in your web method you get a stream and a string with a unique file name, you save the steam into such file name in the destinationFolder above.
if you want to generate a unique name you can create a GUID, I'm sure you will need also the id of the user or so, just to save somewhere who has uploaded this photo.
the destination folder like images/photos should be taken from an app setting in the web.config or from the database, not hardcoded as in my example.
Related
I want to save a file from a http link to the local drive just temporarily in order to access it, this one is working so far and I'm getting the data but a need to write this data to a local file, for example to C:\Windows\temp\test.text, this file should be deleted afterwards.
WebClient client = new WebClient();
string url = "http://www.example.com/test.text";
var file = client.DownloadData(url);
could any one help me on this, thank you!
You cannot write a file on client machine due to security, Any program executing in the browser executes within the browser sandbox and has access to limited features like printer, cookies, etc.
You can write the data to file as a Response object to the client's browser. The Client has the choice of whether to save it or not to his machine.
I am building a method that will build an XLS file and uploading it on user's computer.
I am using this guide:
http://csharp.net-informations.com/excel/csharp-create-excel.htm
So code that will define my destination address is:
xlWorkBook.SaveAs("C:\\Something\\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal);
Now it is default, but i want to allow user to define it by him self, so as far as i understand, i need an html field, which will open common "browse window" and save file path to string, which will be later used in xlWorkBook.SaveAs function. I have read a bit about FileUpload, but i don't really sure that it is what i am looking for.
The code that you have there will save the file on the web server itself, not on the user's computer. You'll need to stream the file down to the user via the browser, and then they will be able to choose where to save it.
You could save the file on the server and then stream it to the user using Response.WriteFile, or you could stream it from memory if you don't want to keep a copy of the file on the server.
This code will create a file on the server, not on the users/clients computer. If you want the user to be able to download the file to his/her computer and select the location where the file is stored, you need to create a file (.aspx file or controller method, depending on wether you are using webforms or MVC) and have it stream the file to the user's browser. The browser will then take care of displaying the "Save as" dialog where the user can select the destination location.
Please help to get file created date from asp.net ascx (ex.)
This is load image from page
Bitmap originalBitmap = new Bitmap(flpContainer.FileContent);
// how to get image **created date** via uploading from local machine (not uploading date)
I want try with FileInfo class, but for asp.net it not work to read a local file properities.
I have functional in web : User upload images from local file system to our application and I want take image created data in File system. I can create new column for created_date in DB, but how I can take created Date image from FS (not created uploading date just created date in local machine via asp.net).
#Eldar, that information is actually lost as it is not transmitted as part of the file upload. You can only get information about the file name. You could request your users to supply a create date as part of the upload or you could set it yourself as part of the upload process to DateTime.Now.
Sorry I couldn't be of more help
I use open XML to export excel file. SpreadsheetDocument.Create requires a file destination.
I want to get this file destination from user similar to a SaveFileDialog in Winforms.
How I get file destination from user? I use Asp.Net 4.0 and OpenXML SDK 2.5 .
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(destination, SpreadsheetDocumentType.Workbook);
For this functionality even if you use open/save dialog, it would show the client locations not the server locations. So here question is, do you want to save that file to the client machine or at the server machine?
If you want to save it to client machine then first save it on the server by any name and location you want and then initiate a download after that. It will automatically prompt download dialog in the client browser and there user would be able to select the location and filename (depending on the client browser settings).
If you want to save it only on server then just before generating the spreadsheet, prompt user for entering the filename (on the page from where the request is generated). If some of your folders are accessible to user then you can also prompt user to select one of the folders, otherwise choose one as per your choice/requirement. Use the filename and the location to generate the spreadsheet.
when a webserver is streaming a file down to the client, you don't have control of the destination folder. All you can do is:
specify the contents type (to aid client in determining how to handle)
specify filename
(I think) tell the client browser to save the file vs. displaying it automatically in the registered application (for example, PDFs usually display in the browser, but setting up the streaming correctly could force Save File dialog instead)
If you need to "cache" the file ion the web server, you export to a server folder that your server process has write permissions to. Then you stream to the client - the client will get the prompt from their browser and save where they want.
Look at Server.MapPath for example - it'll map virtual path to physical on your server. the question of permissions remains.
Depending on your particular case, you may be able to avoid saving to the server, if the export library has a way of returning a byte array or a stream, instead of saving to a file. In that case you just stream the return result to requester.
I am not familiar with the SDK you're using, but quick googling reveals this method of returning the document in a stream:
using (MemoryStream stream = new MemoryStream())
{
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook, true))
{
... work with the spreadsheetDocument, if needed
... prepare and stream to browser
}
}
Here's one of the references to your SDK I found
Use Environment.SpecialFolder enumerations. If you are looking for my documents then:
var destination = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(destination, SpreadsheetDocumentType.Workbook);
Till now I have been storing my uploaded files in upload folder under the website folder. But now the Drive on which the site is stored is full. How do I store new files on another drive and how should I enter the Filepath in DB?
What I do.
Have a domain (eg. domain.com) point to a website folder
Upload files in upload folder under website folder
Store file path in DB as (/upload/filename)
retrieve file with base URL (www.domain.com/upload/filename)
Now if I store files on another drive how do I enter filepath in DB which would return me the file without affecting URL request.
Create a HTTP handler. There is no reason why the URL needs to be related to any physical location or resource.