Iti Tyagi
Iti Tyagi

Reputation: 3661

Unable to download docx file using C#

This is my code and I am trying since hours to download the docx file. but no success. Where I might be lagging, need a slight hint.

if (File.Exists(sTempPath + sCreateFileName))
            {
                FileInfo file =new FileInfo(sTempPath + sCreateFileName);
                Response.ClearContent();
                // LINE1: Add the file name and attachment, which will force the open/cancel/save dialog to show, to the header
                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                // Add the file size into the response header
                Response.AddHeader("Content-Length", file.Length.ToString());
                // Set the ContentType                        
                Response.ContentType = ReturnExtension(file.Extension.ToLower());
                // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
                Response.TransmitFile(sTempPath + sCreateFileName);
                // End the response
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            } 

and Return content type gives, content type for docx file:

"application/ms-word"

where if sTempPath+sCreateFileName is the whole path of the file.

Update: I tried content type:

application/vnd.openxmlformats-officedocument.wordprocessingml.document

This is not working.

Upvotes: 10

Views: 20435

Answers (3)

daniell89
daniell89

Reputation: 2272

I had the same problem. For me it works:

using (FileStream fileStream = File.OpenRead(filePath))
{
    MemoryStream memStream = new MemoryStream();
    memStream.SetLength(fileStream.Length);
    fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);

    Response.Clear();
    Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    Response.AddHeader("Content-Disposition", "attachment; filename=myfile.docx");
    Response.BinaryWrite(memStream.ToArray());
    Response.Flush();
    Response.Close();
    Response.End();
}

Upvotes: 3

Syed Salman Raza Zaidi
Syed Salman Raza Zaidi

Reputation: 2192

Try this code

 string FileName = Path.Combine(Server.MapPath("~/physical folder"), attFileName);
            System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
            response.ClearContent();
            response.Clear();

     Response.AddHeader("Content-Disposition", string.Format("attachment; filename = \"{0}\"", System.IO.Path.GetFileName(FileName)));
            response.TransmitFile(FileName);
            response.Flush();
            response.End();

Upvotes: 3

Roy Dictus
Roy Dictus

Reputation: 33139

The correct MIME type for DOCX is not application/msword but application/vnd.openxmlformats-officedocument.wordprocessingml.document.

The MIME type you specified is for DOC files.

Also you might want to put a Response.Flush() and a Response.End() instead of the CompleteRequest().

Upvotes: 15

Related Questions