2013-08-23 21 views
8

To jest mój kod i próbuję od godziny pobrać plik docx. ale bez powodzenia. Gdzie mogę być w tyle, potrzebuję delikatnej podpowiedzi.Nie można pobrać pliku docx za pomocą C#

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

i daje Powrót typ zawartości, typ zawartości dla pliku docx:

"application/ms-word" 

gdzie jeśli sTempPath + sCreateFileName jest cała ścieżka pliku.

Aktualizacja: Próbowałem Rodzaj treści:

application/vnd.openxmlformats-officedocument.wordprocessingml.document 

To nie działa.

+1

jaki błąd masz? –

+0

spróbuj z typem zawartości jako 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' – Damith

+0

Nie ma błędu, ale plik nie jest pobierany. –

Odpowiedz

8

Prawidłowy typ MIME dla DOCX nie jest application/msword, ale application/vnd.openxmlformats-officedocument.wordprocessingml.document.

Podany typ MIME dotyczy plików DOC.

Możesz również wstawić Response.Flush() i Response.End() zamiast CompleteRequest().

+0

To też nie działa, próbowałem tego samego. –

+0

Iti, zmieniłem odpowiedź. –

+0

Działa to, w praktyce poprzedni kod działa, ale nie w kliknięciu przycisku, ale przy ładowaniu strony. Nie rozumiem, dlaczego tak się dzieje? –

2

wypróbować ten kod

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(); 
+0

Dzięki Syed, to już zrobione. –

1

miałem ten sam problem. Dla mnie działa:

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