提问人:Sawan Meshram 提问时间:10/10/2023 更新时间:10/17/2023 访问量:55
文件“sample.docx”已损坏,因此无法打开,由于文档操作或数据传输损坏,使用 C#
File 'sample.docx' is corrupt therefore can't be opened, cause due to of document manipulation or damage due to data transmission, using C#
问:
经过大量调试,我能够找出文档大小之间的大小差异。
实际上,我正在尝试将文件发送到客户端,这是我的块代码.docx
C#
FileInfo file = new FileInfo(documentFilePath);
byte[] fileConent = File.ReadAllBytes(documentFilePath);
Console.WriteLine("Byte Length :" + fileConent.Length);
context.Response.Clear();
context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", file.Name));
context.Response.AddHeader("Content-Length", file.Length.ToString());
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
context.Response.BinaryWrite(fileConent);
context.Response.End();
上述代码的输出给出了文档文件大小,如下所示
Byte Length :4338
以下是下载文件的客户端代码,如下所示.docx
$.ajax({
type: "POST",
url: '/exportDataForDocument,
data: JSON.stringify(result),
responseType: 'arraybuffer',
success: function(data, textStatus, xhr) {
var fileName = "sample.docx";
var blob = new Blob([data], { type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document" });
console.log("Byte length of the Blob: " + blob.size + " bytes");
//Check the Browser type and download the File.
var isIE = false || !!document.documentMode;
if(isIE) {
window.navigator.msSaveBlob(blob, fileName);
} else {
var url = window.URL || window.webkitURL;
link = url.createObjectURL(blob);
var a_down = $("<a />");
a_down.attr("download", fileName);
a_down.attr("href", link);
$("body").append(a_down);
a_down[0].click();
a_down.remove();
}
}
},
上面的块给出了下载文档文件的字节大小JS
Byte length of the Blob: 7045 bytes
由于服务器上的文档大小与客户端收到的文档大小之间存在大小差异,因此我没有收到相同的文档,而是从服务器接收损坏的文档。
不确定,为什么客户端的文档大小会发生变化?
答:
0赞
Sawan Meshram
10/17/2023
#1
经过一番挣扎,我终于能够解决问题并从服务器下载文件。.docx
对服务器代码进行了一些更改,如下所示:
context.Response.Clear();
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
context.Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}", documentFileName));
context.Response.AppendHeader("X-Request-URL", context.Request.Url.PathAndQuery);
context.Response.TransmitFile(documentFilePath);
context.Response.Flush();
context.Response.End();
并对前端代码进行了如下更改:
$.ajax({
type: "POST",
url: '/exportDataForDocument,
data: JSON.stringify(result),
xhrFields: {
responseType: 'blob'
},
success: function(data, textStatus, xhr) {
var contentDisposition = xhr.getResponseHeader('Content-Disposition');
var fileName = xhr.getResponseHeader('Content-Disposition').split("filename=")[1];
var a = document.createElement('a');
var url = window.URL || window.webkitURL;
var objectUrl = url.createObjectURL(data);
a.style.display = 'none';
a.href = objectUrl;
a.download = fileName;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
url.revokeObjectURL(objectUrl);
},
error: function(e) {
console.log(e)
}
});
最后,我能够从服务器下载具有相同大小文件的正确和正确。.docx
上一个:下拉列表选项未填充
评论