basha
basha

Reputation: 1

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);
}

Upvotes: 0

Views: 4073

Answers (1)

nunespascal
nunespascal

Reputation: 17724

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);
}

Upvotes: 2

Related Questions