Download upload file from specific folder - asp.net

I want to download upload file which is inside a folder into my project..
My Database Table
My Upload Code is
string filename = Path.GetFileName(FileUploadPatDoc.FileName);
string pathName = Path.GetFullPath(FileUploadPatDoc.PostedFile.FileName);
FileUploadPatDoc.SaveAs(Server.MapPath("~/PatientDocuments/") +filename);
fsDocuments.FileName = filename; // fsDocument is Table Object name
fsDocuments.FilePath = pathName;
It works nicely and Store documents in "PatientDocuments" folder which is inside my project.
Now when I want to download it from Gridview using Image-button that times it couldn't find it's destination Path.
Here is the Upload code
ImageButton Btn = (ImageButton)sender;
GridViewRow row = (GridViewRow)Btn.NamingContainer;
string fileName, filePath;
Label file = (Label)grdPatientDocument.Rows[row.RowIndex].FindControl("lblFileName");
Label path = (Label)grdPatientDocument.Rows[row.RowIndex].FindControl("lblFilePath");
if (file != null)
{
fileName = file.Text;
filePath = path.Text;
Response.ContentType = filePath;
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
Response.TransmitFile(Server.MapPath(fileName));
Response.End();
}
It generate following error
"Could not find file E:\CoreZ
IT\June\Hospital\22-06\Final\CZ_BHC\CZ_BHC\BHC\CV_MD.Golam_Shahriar.pdf'"
But I need to find this file from E:....\PatientDocuments\CV_MD.Golam_Shahriar.pdf
Please help me and I am using VS 2012... Thanks

Try this one
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + "");
Response.TransmitFile(filePath);
Response.End();

Related

Why i can not find file on the server for download in asp.net?

I'm beginner in asp.net and want to write simple web application to end user can download any file,write this code for that purpose:
string filePath = "~/beh/" + query[0].OstFileName;
FileInfo file = new FileInfo(filePath);
if (file.Exists) {
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + query[0].OstFileName.Trim());
Response.TransmitFile(Server.MapPath("~/beh/" + query[0].OstFileName.Trim()));
Response.End();
} else {
Response.Write("<script>alert('File Not Found 1')</script>");
}
but when run that code i get else block alert,means get File Not Found 1 message,Where is my fault?thanks all.
string filePath = "~/beh/" + query[0].OstFileName;
In this line you specify the path to search for, including the filename.
The next line you check if this file exists and if so save the file from the query result.
However if you check if the file exists before you actually save the file it will never exist.
Change the path to read: string filePath = Server.MapPath("~\beh").
Then add the filename to that once you save the file.
Hope this makes sense.
Your issue is in the following lines.
string filePath = "~/beh/" + query[0].OstFileName;
FileInfo file = new FileInfo(filePath);
You're giving the FileInfo class a virtual path which the FileInfo cannot map to a local physical path on its own. You are however correctly mapping the virtual path to the local file path when you call Response.TransmitFile.
Change your code to the following:
string filePath = "~/beh/" + query[0].OstFileName;
FileInfo file - new FileInfo(Server.MapPath(filePath));
See: https://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx

Opening a word file after downloading from code behind(asp.net)

I have written code to open a Word document after downloading it in code behind. The document is opening fine, but it is not saving in Word format. When I am going to open it, it is asking for selecting the format to open the file.
The code is below:
string FullFilePath = "D:\\ASP\\ASP.doc";
FileInfo file = new FileInfo(FullFilePath);
if (file.Exists)
{
Response.ContentType = "application/vnd.ms-word";
Response.AddHeader("Content-Disposition", "inline; filename=\"" + txtDate.Text + "\"");
Response.AddHeader("Content-Length", file.Length.ToString());
Response.TransmitFile(file.FullName);
}
Set your content type to application/msword.
Refer: Microsoft Office MIME Types
You are not specifying an extension when sending the file name.
If your file saves without an extension, you will get the prompt asking for the application to use to open it.
Also, use the "Content-Disposition", "Attachment" if you want to tell the browser to save the file. inline will make the browser attempt to open the file in Word directly.
string FullFilePath =//path of file //"D:\\ASP\\ASP.doc";
FileInfo file = new FileInfo(FullFilePath);
if (file.Exists)
{
Response.ContentType = "application/msword";
Response.AddHeader("Content-Disposition", "Attachment; filename=\"" + txtDate.Text + ".doc\"");
Response.AddHeader("Content-Length", file.Length.ToString());
Response.TransmitFile(file.FullName);
}

asp.net downloading file with french character

I need to download a file with french file name for example "mé.txt"..I have this code:
FileStream fileStream = File.Open("filePath", FileMode.Open);
byte[] bytContent = new byte[(int)fileStream.Length];
fileStream.Read(bytContent, 0, (int)fileStream.Length);
fileStream.Close();
string fileName = "mé.txt";
Response.AddHeader("Content-disposition", "attachment; filename=" + fileName);
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = Encoding.GetEncoding("ISO-8859-1");
Response.BinaryWrite(bytContent);
But the problem is that when I have the pop up window to save my file .. Im getting this name : mé.txt
How can i fix it?
i think the problem has to be solved on the serverside using HttpUtility.UrlPathEncode

when i try to save this file it creates and folder for the entire path

ZipFileToCreate = "c:\user\desktop\webservice\file.zip";
So, when i try to save this file it creates the folder for the path like user\desktop\webservice\file\
Why is it so?
FileStream fs = new FileStream(ZipFileToCreate, FileMode.Open);
byte[] data = new Byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
br.Read(data, 0, data.Length);
br.Close();
Response.Clear();
Response.ContentType = "application/x-zip-compressed";
Response.AppendHeader("Content-Disposition", "filename=" + Parameter + ".zip");
DeleteOldFiles();
Response.BinaryWrite(data);
I think you need to be using the Environment.GetFolderPath method to find the current users' Desktop folder rather than hard-coding "c:\user\desktop\webservice\file.zip". Also using Path.Combine to build a path is more reliable than string concatenation. Try
Parameter = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "file.zip");

ASP.NET Create zip file for download: the compressed zipped folder is invalid or corrupted

string fileName = "test.zip";
string path = "c:\\temp\\";
string fullPath = path + fileName;
FileInfo file = new FileInfo(fullPath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
Response.AppendHeader("content-length", file.Length.ToString());
Response.ContentType = "application/x-compressed";
Response.TransmitFile(fullPath);
Response.Flush();
Response.End();
The actual zip file c:\temp\test.zip is good, valid, whatever you want to call it. When I navigate to the directory c:\temp\ and double-click on the test.zip file; it opens right up.
My problem seems only to be with the download. The code above executes without any issue. A file download dialog is presented. I can chose to either save or open. If I try to open the file from the dialog, or save it and then open it. I get the following dialog message:
The Compressed (zipped) Folder is invalid or corrupted.
For Response.ContentType I've tried:
application/x-compressed
application/x-zip-compressed
application/x-gzip-compresse
application/octet-stream
application/zip
The zip file is being created with some prior code (that I'm sure is working fine due to my ability to open the created file directly) using: Ionic.zip
http://www.codeplex.com/DotNetZip
This worked. I don't know why but it did.
string fileName = "test.zip";
string path = "c:\\temp\\";
string fullPath = path + fileName;
FileInfo file = new FileInfo(fullPath);
Response.Clear();
//Response.ClearContent();
//Response.ClearHeaders();
//Response.Buffer = true;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
//Response.AppendHeader("Content-Cength", file.Length.ToString());
Response.ContentType = "application/x-zip-compressed";
Response.WriteFile(fullPath);
//Response.Flush();
Response.End();

Resources