下载后,在asp.net中下载.docx文件已损坏。 [英] Downloading .docx file in asp.net is being corrupted after download.

查看:87
本文介绍了下载后,在asp.net中下载.docx文件已损坏。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在asp.net上传了一个docx类型的文件,iam尝试使用以下代码下载它:



 如果 e.CommandName =  下载 然后 
Dim fst 作为 FileStream(Server.MapPath(e.CommandArgument),FileMode.Open)
Dim fl 作为 字符串 = e .CommandArgument
Dim file As String ()= e.CommandArgument.ToString.Split(
< span class =code-keyword> Dim extension < span class =code-keyword> As String = file(file.Length - 1
Dim bytBytes(fst.Length) As 字节
' 逐字节读取文件
fst .Read(bytBytes, 0 ,fst.Length)
fst.Close()
Response.AddHeader( Content-disposition 附件; filename =& fl)

如果 extension = docx 然后
Response.ContentType = application / vnd.ms-word.document
ElseIf extension = pdf 然后
Response.ContentType = Application / pdf
ElseIf extension = xls 然后
Response.ContentType = application / vnd.ms-excel
ElseIf extension = xlsx 然后
Response.ContentType = application / vnd.ms- excel
ElseIf extension = doc 然后
Response.ContentType = application / ms-word
结束 如果


' 将所有字节写入新创建的文件
Response.BinaryWrite(bytBytes)
响应。结束()
结束 如果





除了docx之外的所有其他文件都是下载并打开,但docx文件已下载并损坏。所以如何修复它?



谢谢。

解决方案

更改响应。内容类型。



内容类型:application / vnd.openxmlformats-officedocument.wordprocessingml.document 


为什么使用Response.BinaryWrite()?



检查这个...... < br $> b $ b

http://www.c -sharpcorner.com/UploadFile/Globalking/filedownload0227200605205​​2AM/filedownload.aspx [ ^ ]


这对我有用。试试!!



string filePath = Server.MapPath(File / Test.docx); //使用(FileStream fileStream = File.OpenRead(filePath))指定.docx文件的位置



{

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

}

Hi all ,

I uploaded a file of type docx in asp.net and iam trying to download it using the following code :

If e.CommandName = "Download" Then
           Dim fst As New FileStream(Server.MapPath(e.CommandArgument), FileMode.Open)
           Dim fl As String = e.CommandArgument
           Dim file As String() = e.CommandArgument.ToString.Split(".")
           Dim extension As String = file(file.Length - 1)
           Dim bytBytes(fst.Length) As Byte
           'read file byte by byte
           fst.Read(bytBytes, 0, fst.Length)
           fst.Close()
           Response.AddHeader("Content-disposition", "attachment; filename=" & fl)

           If extension = "docx" Then
               Response.ContentType = "application/vnd.ms-word.document"
           ElseIf extension = "pdf" Then
               Response.ContentType = "Application/pdf"
           ElseIf extension = "xls" Then
               Response.ContentType = "application/vnd.ms-excel"
           ElseIf extension = "xlsx" Then
               Response.ContentType = "application/vnd.ms-excel"
           ElseIf extension = "doc" Then
               Response.ContentType = "application/ms-word"
           End If


           'write all the byte to newly created file
           Response.BinaryWrite(bytBytes)
           Response.End()
       End If



All the other files except the docx are being downloaded and opened,but the docx file is downloaded and corrupted.So how to fix it ?

Thanks .

解决方案

change the response.Content type.

Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document 


Why are you using Response.BinaryWrite() ?

Check this.....

http://www.c-sharpcorner.com/UploadFile/Globalking/filedownload02272006052052AM/filedownload.aspx[^]


This worked for me. Try out!!

string filePath = Server.MapPath("File/Test.docx"); // Specify the location of the .docx file
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();
}


这篇关于下载后,在asp.net中下载.docx文件已损坏。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆