ASP.NET: Create CSV and save file on client machine - c#

Requirement: I have an ASP.NET application where a page has data displayed in gridview. This form also has a textbox which takes filepath as input, with a save button beside it. Now when the user clicks on save the csv should save it on client machine at the path the user entered.
I tried opening up a SaveAs Dialogbox using the "Content-Disposition attachment filename". But it always opens up on its default path. Is there a way to open up this SaveAs Dialog on user-specific path.
Else is there any other way of creating file on server and copying it to client machine?
Please suggest.
-Justin Samuel.

This cannot be done, because of security restriction. Let the user download the CSV and choose the location on his own.

If you are trying to force the download to always give a Save As, you should set the content-type to application/octet-stream. However, it will always be up to the user to decide where that file is saved and that is as it should be for security reasons.

Related

Passing dynamic data with click once application

I have a web page where the user provides the input and press the Submit button. On button click I'm downloading the click once application to process the input on client machine. Currently passing the input as query string parameter to the click once application.
i.e. myapp.application?
But there is a limitation in the Query string, that I cannot send the data more than ~2000 characters, Is there any way that I can attach the data along with the download of click Once application?
ClickOnce uses manifest file to define, what files should be downloaded. I hope, you are using signed manifest for application deployment. Signed manifest contains package files hashes as well as hash of the manifest itself. You can put the data into the file, which is defined in the manifest. However, you will have to generate manifest file dynamically every time a user requests application installation along with new portion of data. This can work fine, if the user removes the application right after data processing has been finished. But it can easily out of control eventually.
I would try another approach: store the data on server side and put id value into query parameter string. The application can request the data from the server by provided id. Just make sure that the data is protection is consistent with its sensitivity. For example, if it is some kind of private user information or data protected by law, you need to make sure that only authorized user can get it from the server and the data is stored in secure manner.

asp.net check if a file from the Response.OutputStream has been saved/opened

I am building an excel file (using EPPLUS) on a web page which gives the option to either save or open the file.
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=Excel_List.xlsx");
package.SaveAs(Response.OutputStream);
Response.End();
That works fine. I just want to know how can I check if the user has either opened/saved the file?
Is there an event for that?
The simplest answer is you cannot. Once the server has handed the file off to the client ASP.NET has no control over it.
You could, in theory, place a macro in the Excel file to call a URL on your server, but that's assuming the user opens the Excel file and allows for the execution of the macro (which is disabled in modern Excel versions).
You cannot determine it. After the output is generated you have no info about download process or actions that user takes on downloaded file. It is caused mostly by web browser security limitations.
No, that is not what HTTP is for. If you can successfully send all data from the server, you may assume the client has received it all. Client may then discard, save, print, copy, do whatever they like with the data.
If you want to perform such a check, you should do it from the Excel file itself. But then again you shouldn't, because of privacy. If I ever caught an Excel file phoning home, I don't know what'll happen.
It isn't possible to know if they actually opened it in Excel without adding a macro to the file, but there are ways that you can send a confirmation to the server that the download was completed successfully. For this you would need to embed a rich client in the browser to handle the HTTP request rather than doing it with the browser alone. Something like SoftArtisans XFile can be used for this. Our .NET Excel library also comes with an ActiveX control that can manage the download process (called the OfficeWriter Assistant).
Disclaimer: I work for SoftArtisans

how to create folder on server from code in c#?

Is there any way to show user popup to give the name to create folder
And with that if he does not want to create folder then he should be shown the list of folders to which he want to save the file
Is it proper way that I create a popup with javascript with the list of folders on my server and put a textbox and button to save the new and in code we will create directory in code passed with textbox?
string pathToCreate = "~/UserFolders/" + TextBox1.Text;
if(Directory.Exists(Server.MapPath(pathToCreate))
{
//In here, start looping and modify the path to create to add a number
//until you get the value needed
}
//Now you know it is ok, create it
Directory.CreateDirectory(Server.MapPath(pathToCreate));
string targetPath = Server.MapPath("FolderName"); //with complete path
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
fileToUpload.PostedFile.SaveAs(targetPath + ImageFileName);
}
else
{
fileToUpload.PostedFile.SaveAs(targetPath + ImageFileName);
}
As I mentioned by commenting that Functionality like SaveFileDialog is only possible in Windows Forms application, you cannot do this in asp.net
The alternatives in asp.net are difficult however as an alternate there is a third party editor available on the web called CKEditor 3.x Developer's Guide.
You can use File Browser (Uploader) for the purpose you want to achieve.
to create folder :
System.IO.Directory.CreateDirectory(newPath);//newpath is your folder creating path
If you are developing web applications (I can see you have tagged your question as asp.net), you do not need to worry about the pop up window. You just send the file through the Response and the download window will be shown by the browser.
Using the Example given in How to Create a Folder or File You can create a folder, but in asp.net you can not Use open file dialog or save file dialog functionalities, this will work only in C# Windows forms application.
Web applications and the HTTP protocol do not support direct manipulation of the server-side file system. In fact, that would be a dangerous idea.
It sounds like you have a base folder somewhere on the web server where you want users to be able to upload files, with some user control over subfolders and location of the uploaded file. I would suggest dong this by presenting the file system in a tree view or a list of links (where the links let you navigate up/down the folder tree). Combine this with a file input and you've got yourself a solution. HOWEVER, be very careful to control and cleanse the specified file name for the uploaded file. There are many combinations (such as utilizing "..") that can allow the user to hack into unwanted file locations on your server.

New opened window opens but closes immediately

I'm trying to download file from FTP using javascript, for which I created the following topic:
Is it possible to download file from FTP using Javascript?
From there I learned that I can use window.open('ftp://xyz.org/file.zip'); to download the file. It opens a browser new window, but the window closes immediately.
How I can I force it to stay open?
Actually I do all these in Silverlight application:
Here is the code:
HtmlPage.Window.Eval("window.open('" + url+ "', 'Download', 'height=500,width=800,top=10,left=10');");
I also tried this,
string targetFeatures = "height=500,width=800,top=10,left=10";
HtmlPage.Window.Navigate(new Uri(url), "_blank", targetFeatures);
But both results in same : it opens a window, and closes it immediately. I see it just for fraction of second!
I know this doesn't answer your question, and I'm sure you know all of this. I'm answering more because I don't see this point brought up often. :)
Silverlight has very limited support for client interactions. Javascript is a shim that in my opinion gets overused to try and bypass things that Silverlight was architectured against. It would have been very easy for Microsoft to include FTP support in Silverlight but it was excluded for a reason.
However, Silverlight has great support for webservice interactions. So the recommended way of getting a file would be to call a webservice that would do the FTP transfer for you and then send the contents down to the Silverlight application via the webservice. Possibly even processing it on the webservice side for any business logic etc.
Like I said, I suspect your requirement is to not use a webservice (to pass the bandwith cost onto the user most likely). But it'd be interesting to know more about your business problem instead of your technical problem for the solution you've chosen.
It closes because it triggers file download. You can open two windows - one for message and one to download file, but I thiunk user will know it is downloading...
If I were you, I'd open up a page that has whatever visual/UI stuff you'd want to show the user, and either have a META tag that redirects to the download URL, or has a javascript blurb to fire off said download. That way, your window will stay open, but the download will still start automatically.
to keep it open use
var test = window.open();
test.location = 'ftp://openbsd.org.ar/pub/OpenBSD/2.0/arc/kernels/bsd.ecoff';
and to not open any window use
window.location = 'ftp://openbsd.org.ar/pub/OpenBSD/2.0/arc/kernels/bsd.ecoff';
or make a normal link
Remember that a browser is not meant to "display" (visually anyway) the FTP protocol, and not all browsers will suport it. If you want to allow the user to download something, consider using a normal http:// protocol, and opening a window normally as others have suggested.
If you really need the download to be hosted via FTP, consider your backend ingesting (and caching) the file and return it to the user via http
There is nothing to be parsed on the browser's side, hence it closes. If you want to have the page open, you'll have todo something dirty. Like creating a html (or php) page and serve the content you want the user to see, then with a hidden i-frame which will call the FTP contents.
This way your user will see the content you want them to see, and the file is being downloaded.
I had the exact same problem, Silverlight opening a new window for downloading a file would flash a blank window up briefly and it would disappear again without the file download occurring.
This seemed to happen in IE 8 (not 9 and up) and could be fixed by going into Tools->Internet Options->Security then click Custom level... (for whatever zone your site would be in) and go to Downloads->Automatic prompting for file downloads and make sure this is Enabled (I also have File download enabled below that). This Automatic prompting for file downloads setting seems to be absent from IE 9+.
Another workaround is to not open in a new window, if the target url immediately downloads a file it won't change the current window so there's no difference in UX:
HtmlPage.Window.Navigate(new Uri("\download.ashx?fileid=12345"));

How to open a file stored in a server from aspx page using UNC share path?

I am listing the files stored in a remote server in the aspx page grid view with the file name linked (href) to the UNC path of the file.
when the user clicks the file name link, it should open the file from the server to allow the user read the file.
I am using impersonation (Using mirror logon method) for file upload and delete in the same page which works fine.
When I try to click the link to view the file it throws an error message box saying access denied.
While upoading / deleting file from my application, it uses the impersonated account I specified in web.config.
But it uses default ASP.NET (SERVER\IUSR_SERVER) account when trying to access via the link.
I cant give read access to all in my file server share.
How can I make the application use the impersonated account while trying to access the file for reading through the link I am showing for the files?
Or please suggest me an better alternative to do the same.
Thanks In advance.
When you generate the links in the Web page, do not point directly to the UNC, but to a page of your app, with a parameter to identify the file. In this page, use the same impersonation method to open the file and send its content back to the Web browser.

Categories

Resources