I would like to give my users an option to select a directory path on their local machine (not select an actual file).
This is so they can save xml files into a directory on their hard drive. When user selects the directory I want to pass that path to my code which then loads data from the xml files into my database.
I know I can get the users to select a file:
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
But this is not what I want, I want them to just select the directory.
Can someone advise if this is easily done in an MVC application.
A web application cannot directly write to a user's computer. The best you can do is provide a downloadable link and the user needs to do a SAVE AS from the browser.
In your case, forget about the directory path and just give a link/button on your web page to get the XML files.
On click, generate the XML file from the Database and use the following snippet for the user to get a download prompt.
// code on the click of the button/link
FileInfo file = new FileInfo(tempFilePathOnServer);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + file.Name + "\"");
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "text/xml";
Response.Flush();
Response.TransmitFile(file.FullName);
Response.End();
Related
On site (ASP.NET) i have table with items. Every item has link to specific pdf. This pdf i generate from binary data that are saved in database. So realy, i don't have path to pdf file.
When i click on link, i want to download file. And this functionallity work fine.
But i download file to default location that use browser (By default it's C:\Users\Admin\Documents)
What should i do to make browser show save dialog where user can chose destination path for pdf?
Code to generate pdf file :
private void ProcessFile(byte[] file, string fileName)
{
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition",
string.Format("attachment;filename=\"{0}\"", fileName));
Response.BinaryWrite(file);
Response.Flush();
Response.End();
}
About my code : i'm going to rewrite a bit it according to this post
i'm trying to write code that download a file that located on the server.
but the save as dialog wont open in IE.
i tried response.redirect, i tried
Response.Clear();
Response.ContentType = "text/csv";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileNameDisplay);
Response.WriteFile(Server.MapPath(pathName + fileNameUnique));
Response.Flush();
Response.End();
every thing works on firefox and chrome , but not in internet explorer.
i know that there is a security option for this in security ---> custom level ---> Downloads ---> automatic prompting for file downloads , that is always in disable mode and i need to switch it to enable in order it to work, but i don't wont that my users deal with this.
how do i overcome this 'security problem' ?
is there any right way to deal with download files ?
what is the right code to do that ?
thank you,
gadym
var info = new FileInfo(path);
Response.Clear();
Response.AppendHeader("Content-Disposition", String.Concat("attachment; filename=", info.Name));
Response.AppendHeader("Content-Length", info.Length.ToString(System.Globalization.CultureInfo.InvariantCulture));
Response.AppendHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "text/csv";
Response.WriteFile(info.FullName, true);
Response.End();
You need explicitly tell that it is not opened content,
add following headers:
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fname);
Response.AppendHeader("Content-Transfer-Encoding", "binary");
Also it is strongly recommend to set explicit file length
Response.AppendHeader("Content-Length", responseContentLength.ToString());
abatishchev, thank you for helping me.
i found a soultion to my problem.
I created a dialog window (let's call it 'DownloadWindow') which holds an empty 'A HREF' tag.
(it's shows - 'click here To Download')
after i click a download (EXCEL/CSV icon) button on my default page (which creates my file
dynamicly),
i load the dialog window ('DownloadWindow') and then i'm repleacing the 'a href' link to the file url i created earlier so my users can download it from my server.
Now, Internet explorer did pop up the open/save/cancel dialog box.
it's a bit annoying but it's solved my porblem.
I had the same problem when my link for file download was made using:
<a href="DownloadFile.aspx">
<input type="image" src="~/virtual/path/to/image.png" runat="server" />
</a>
The problem was gone when i changed it to:
<a href="DownloadFile.aspx">
<img src="~/virtual/path/to/image.png" runat="server" />
</a>
I have ASP.NET page with an iframe on it for displaying some pdf reports on this page.
When user select the report type from dropdown, I add the needed for report data into the ASP.NET Session and change the attribute "src" of the iframe to .ashx module address which generates the .pdf report.
But if Adobe glug-in for viewing .pdf files in browser is not installed, browser proposes to save report file and name of the proposed file is "HandlerName.ashx".
But I want to make the browser proposed to save the file with the name "Report.pdf". Can I do this? Are there any workarounds?
Add the following header in your ashx-file:
context.Response.AddHeader("content-disposition", "attachment;filename=PUTYOURFILENAMEHERE.pdf");
context.Response.ContentType = "application/pdf";
Try inline content-disposition, however I have to check this:
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "inline; filename=" + name );
Use
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=Report.PDF");
Response.WriteFile(Server.MapPath("~/YourPath/Report.PDF"));
Response.End();
I have retrieved the pdf file from Database and linked it to a "a href" tag to open pdf file using link. Now I want to give the open/save dialog box before a pdf file opened . - in javascript(by using onclick event in "a" tag to call the javascript)
You have to set the content-disposition header using C# to get this behavior in a browser.
Read this for more info
How do I prompt a "Save As" dialog for an accepted mime type?
Downloading a File with a Save As Dialog in ASP.NET
Example
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.AddHeader("Content-Length", targetFile.Length.ToString);
Response.ContentType = "application/pdf";
Response.WriteFile(targetFile.FullName);
How can i show a Save as window in asp.net and C#...
This is similar to the one that opens for uploading but that window is the open file...
I am not looking to save only one file...like
Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
can i make it ung ajax... please help... samples or suggestions..
thanks
As far as I know you don't have any way of interacting with the file system using javascript from inside the browser. I would imagine that this would be a fairly large security hole if you could. The best I can suggest is to package up your files in a zip file then send that in a response with the content disposition set to attachment, then let the browser handle it for you.
In your page:
<iframe src="getFile.aspx?id=1"/>
<iframe src="getFile.aspx?id=2/>
Then inside getFile.aspx
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
Response.BinaryWrite(fileContents);
(Note: never tried this myself...)