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
Related
In my project the user can search the details of the school students and export the same to excel. I am using OpenOfficeXMl for achieving this.
The excel is downloading with the data once the search button is clicked.
Is it possible to redirect to an another page after the excel download is completed.
I am assigning the data to a datatable, then it is extracting to the excel as below.
using (ExcelPackage pck = new ExcelPackage())
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("PData");
ws.Cells["A1"].LoadFromDataTable(p_data, true);
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=School-" + DateTime.Now + "-file" + ".xlsx");
Response.BinaryWrite(pck.GetAsByteArray());
Response.End();
}
Is it possible to do a return View() or Redirect after Response.End()?
Edit
After the excel is downloaded I need to navigate to another page to show the same data which is exported in excel. like return View(Data)
where Data is a IpagedList Object
You cannot send an excel file to the response and at the same time redirect.
In this case, an approach could be to simply return a View, and pass a view model that contains your IpagedList as well as the link to your pdf. No need for a redirect; unless your previous action was a POST.
In the resulting page you then can show the IpagedList information and also a link they can click download the excel file, or even download this automatically from jquery, for example by opening a new window with the URL pointing to your pdf.
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();
I have a gridview in which I have provided an option for the user to download the pdf files. When they click on the pdf icon sometimes it open the pdf file in a new tab and sometimes it starts downloading. How can i make it download always?
You need to add a button (image button, linknbutton or button) and handle the RowCommand event of GridView. In RowCommand handler you may write code to download a file.
You may use Response object's method.
string filepath=MapPath("~/files/file.pdf");
byte []bytes=System.IO.File.ReadAllBytes(filepath);
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Type", "application/octet-stream");
Response.AddHeader("Content-Length", bytes.Length.ToString());
Response.AddHeader("Content-Disposition","attachment; filename=file.pdf");
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
In order to always force a download you need to add the Content-Disposition header as AVD showed; however, I find this totally unnecessary; I think it would suffice to have the link to the PDF open in a new window. In other words, have target="_blank" defined. Example:
invoice
Then, is up to the user whether he wants to save the file locally or just see it on the screen. I think the important thing is that this won't interfere with the current page the user is looking at.
I have a file that is in .pdf format for my website.
If you left click on the link, it will open the Adobe Reader and open the file.
What I want to do is when you left click, it opens a dialogue box that asks you where you want
to save the file.
I know you can do this by right clicking and choosing save as,
but is there any way by just
one simple left click to downlaod it?
Im sure there is some sort of hack to accomplish this but this is the feature that is installed with the browser when Adobe Reader is installed on the machine. Keeping this consistent for all users is the best approach. Do not bother with trying to make the left click open a save dialog. Users are accustomed to this behavior and will know how to save the PDF to thier local hard disk
Consistency is the key here.
two parts, you have to convert the pdf to a byte stream and add a content-disposition header to the html response.
first get the file and stream to a byte array
public byte[] GetDocument(string filePath)
{
return File.ReadAllBytes(filePath);
}
Then send it - as the very first thing in the Page_Load event - you'll need to pass the path to the file through session or query string to a new page, as you can't change the content type on an existing page you have already viewed in the browser.
Byte[] fileData = GetDocument(filePath);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/pdf";
// prompt to download
Response.AppendHeader("content-disposition", "attachment; filename=declarations.pdf");
Response.AppendHeader("content-length", fileData.Length.ToString());
Response.BinaryWrite(fileData);
Response.Flush();
Response.Close();
Add the pdf to a zip folder and add a link to the zip folder
Remove the pdf location from the href part of your link, and code an onClick method instead.
<a href="#" onClick="$:openSaveDialog">
<script>
function openSaveDialog() {
//Write a JavaScript function to prompt the user with a Save Dialog Box.
}
</script>
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);