I have a StringBuilder object which I have built with comma seperated values, which I would like to save as a .csv file.
I know how to stream the data as CSV, but how can I physically save the data as a .csv file, on the server?
Currently I stream the CSV like the following, where sb is StringBuilder:
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment;filename=List.csv")
Response.ContentType = "text/csv"
Response.Write(sb.ToString)
Response.End()
I assume you already know how to create csv, so check this question that shows how to save stream to file. As you're using ASP.NET just make sure you have the appropriate permissions to modify.
Conclusion
As you want to make the download available to user(instead of save it in the server) and csv is basically a text file check out this
It's not different than writing a normal file. If you want to save the file to a folder in your application, you should use Server.MapPath("~\MyCsvFiles") to get the physical path of the file.
Depending on the version of IIS, ASPNET or NETWORK SERVICE needs write permissions on the server folder you are writing to; then just write the CSV file out with StreamWriter or your preferred method. If you really want an Excel file, then consider using EPPlus.
Related
I have a website that creates a txt file and saves it with a timestamp to the webserver directory.
I need a client based app to list all files in the directory to download to the client server for processing but cannot find a way to have them listed in a listbox without specifying the full file name
i.e. TB2014-09-08_11h48m25_765.txt is a full name. TB stays constant and the files are always .txt)
you want the GetFiles method in the Directory class (System.IO namespace). Something along these lines:
Dim files As String() = Directory.GetFiles("c:\YourFolder", "TB*.txt")
For Each filename In files
Console.WriteLine(filename)
Next
I have developed File Upload web page in ASP.NET. Now user can rename a .exe file to txt or some other extension and upload the same. I want to restrict that. How I can implement that in ASP.NET?
The only safe way to do this is to get the byte [] from the file that has been posted and examine it to determine if the file is indeed in one of the formats you allow the user to upload. You don't need to save the file, you can just get the byte[] from the HttpPostedFile object.
Other than examining the content (looking for magic numbers, for example) there isn't an infallible way to make sure that the user is not attempting to upload something that you don't allow.
I have a ASP.net page which writes a file to the local disk.
I want to present the user a Save File dialog box and allow him to set the path to the folder.
I know code like below can be used;
Response.Clear();
Response.ContentType = "text/csv";
Response.AddHeader( "Content-Disposition", "attachment;filename=\"report.csv\"" );
// write your CSV data to Response.OutputStream here
Response.End();
But it fixes filepath.
I need to capture the filepath that the user selects.
Is that possible in ASP.net?
Thanks.
it does not work like that from a web page, you have to initiate the download suggestiong a target file name then the user can override your suggested file name and select any folder or filename he likes and your content will be saved in that location.
you do nothing with a local path which only makes sense on the client machine on the server side of ASP.NET application.
I need to capture the filepath that the user selects. Is that possible
No. Your web server presents a file to the client, where the client has the option to save this file.
In what way would the path the client saves this file be interesting to the server?
I have an ASP.NET 2 application and would like users to upload a file to be processed immediately. The file never needs to be used again, so I don't care to store it on the server as a file somewhere, which hopefully will make it more secure on our end.
Basically, they upload an excel file and I process it and display some results. I do not care to save that excel file for later.
How can I do this?
You can hold the file contents in a MemoryStream.
This will ensure it is not saved to disk.
See What is the best practice for storing a file upload to a MemoryStream (C#)? for details.
I am trying to create a zip file and save it using DotNetZip library in ASP.NET application.
But for some reason i get a
Access to the path is denied
error when i try to save it.
I changed the TempFileFolder to another folder and have given permissions to it. Still no luck.
Dim zipFile As New ZipFile()
zipFile.AddFile(filePath)
Dim tempFilePath As String = "Report.zip"
zipFile.TempFileFolder = "D:\Temp\"
zipFile.Save(tempFilePath);
I found a question but the answer did not help me.
From the above question, one answer mentioned:
Also, the tempFilePath in your example doesn't include a full path, could it be that it is trying to save the ZIP into a different folder from the one you are expecting (and have assigned permissions to)?
How to figure out to which folder it is trying to save even though I mentioned TempFileFolder as D:\temp\?
Any thoughts?
Since you said you 'gave permissions' I'm assuming that you provided the account(s) which run the ASP.NET and IIS processes file Read/Write permissions to the folder where you're trying to save this file.
The 'Temp File Folder' is just what its name describes: a temporary file folder. It's a holding place in case the library needs to do some file I/O. it's not a base file.
Modify the code to provide a fully qualified path name to save the file to:
Dim zipFile As New ZipFile()
zipFile.AddFile(filePath)
Dim tempFilePath As String = "D:\Temp\Report.zip"
zipFile.TempFileFolder = "D:\Temp\"
zipFile.Save(tempFilePath)
Check if the file is not Read Only