2013-08-23 15 views
8

Questo è il mio codice e sto cercando da ore di scaricare il file docx. ma nessun successo. Dove posso essere in ritardo, ho bisogno di un leggero suggerimento.Impossibile scaricare il file docx utilizzando 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(); 
      } 

e ritorno tipo di contenuto dà, tipo di contenuto per il file docx:

"application/ms-word" 

dove se sTempPath + sCreateFileName è l'intero percorso del file.

Aggiornamento: ho provato tipo di contenuto:

application/vnd.openxmlformats-officedocument.wordprocessingml.document 

Questo non funziona.

+1

quale errore hai? –

+0

prova con il tipo di contenuto come 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' – Damith

+0

Non c'è nessun errore ma il file non viene scaricato. –

risposta

8

Il tipo MIME corretto per DOCX non è application/msword ma application/vnd.openxmlformats-officedocument.wordprocessingml.document.

Il tipo MIME specificato è per i file DOC.

Inoltre, è possibile inserire uno Response.Flush() e uno Response.End() anziché lo CompleteRequest().

+0

Anche questo non funziona, ho provato lo stesso. –

+0

Iti, ho modificato la mia risposta. –

+0

Funziona, infatti il ​​codice precedente funziona ma non nel clic del pulsante, ma al caricamento della pagina. Non capisco perché questo sta accadendo? –

2

provare questo codice

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

Grazie Syed, è finito. –

1

Ho avuto lo stesso problema. Per me funziona:

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