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);
}
Related
I want to download the parent Zip file which contains more zip files in asp.net c#.
Following code work for single zip file which have more .pdf,.txt ect.. files.
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
zip.AddEntry(filename, "");
Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=" + "filename.zip");
zip.Save(Response.OutputStream);
Response.End();
I'm creating an excel file using Respose.Write in ASP.NET C#, my users can save the created file on their systems, but I want to save this excel file on my server, without user knowing anything about it. how is it possible? this is how I create my Excel file:
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" + "res" + ".xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
....
Response.Write("</table>");
Response.Flush();
Response.End();
this code saves created excel file client PC, but I want to save created file on the server
The general idea is to write the contents of the Excel file to a buffer. Then a) save it to a file and b) stream it to the client. You will want to use a unique file naming convention on the server (include a time stamp for example). The IIS App Pool also needs write permission on the folder where you are saving the file to.
StringBuilder excel = new StringBuilder();
excel.Append("<table>");
...
excel.Append("</table>");
// save to file
File.WriteAllText("C:\\blah.xls", excel.ToString());
// output to response
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" + "res" + ".xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
Response.Write(excel.ToString());
Response.End();
I try to download a docx file from serverside.
What is my wrong ?
this is code :
FileInfo file = new FileInfo(filepath);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AppendHeader("Content-Disposition", "attachment; filename = " + ((Button)sender).CommandName + ".docx");
Response.AppendHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.Flush();
Response.Close();
Response.End();
I have posted something similar in another question for an PDF but here goes. It's much easier to stream this sort of data back through an ASHX handler.
Something like what I posted in this question but with a docx file.
Display PDF in iframe
It looks like you are using a normal ASP.NET page and are trying to modify the standard behavior by clearing out the headers, etc. You won't have to fiddle with the headers or anything like that with an ashx handler.
I am exporting a excel file from "c:Test\data.xls" using the responce object
like:
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
it is opening a dialog box.But Now we don't need the dialog box.we want to directly open file without dialog.
Can any body help me out
use
response.AddHeader("Content-Disposition", "inline; filename=" + FileName + ";");
Whether it opens directly depends on client-side security settings to this might work but there is no guarantee...
Instead of attaching file to response you nee write it's content to response stream.
//Set the appropriate ContentType.
Response.ContentType = "application/vnd.ms-excel"; // not sure in content type
//Get the physical path to the file.
string filePath = #"c:Test\data.xls";
//Write the file directly to the HTTP content output stream.
Response.WriteFile(filePath);
Response.End();
But also it depeneds on the client side settings.
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();