[英]Response received from POST corrupted
I have a service called DownloadZipFile that collates data and then builds a Zip file for it to be downloaded. 我有一个名为DownloadZipFile的服务,用于整理数据,然后构建一个Zip文件以供下载。 this service returns a response containing the stream to the file 此服务返回包含该文件流的响应
[HttpPost]
public ActionResult DownloadZipFile(string zipData)
{
try
{
// Create the Zip File.
using (MemoryStream zipStream = DownloadHelper.BuildZipFileData(zipData))
{
// Build up the reponse including the file.
HttpContext.Response.ClearContent();
HttpContext.Response.ClearHeaders();
HttpContext.Response.Clear();
HttpContext.Response.Buffer = true;
HttpContext.Response.ContentType = "application/zip";
HttpContext.Response.AddHeader("Content-Disposition", "attachment; filename=MyZipFile.zip;");
HttpContext.Response.BinaryWrite(zipStream.ToArray());
HttpContext.Response.End();
}
}
catch (Exception e)
{
// Log the error.
_logService.Error(LogHelper.GetWebRequestInfo(ControllerContext.HttpContext.Request), e);
}
}
It would download and open the zip file correctly if I call the service like this. 如果我这样调用这个服务,它会正确下载并打开zip文件。
var form = $("<form></form>").attr('action', "DownloadZipFile").attr("method", "post");
form.append($("<input></input>").attr("type", "hidden").attr("name", "zipData").attr('value', escape(JSON.stringify(zipData))));
form.appendTo('body').submit().remove();
However, if I use an AJAX Post call when converting it from response to blob, the size is much larger than what I sent. 但是,如果我在将其从响应转换为blob时使用AJAX Post调用,则其大小远大于我发送的大小。
$.post("DownloadZipFile", { zipData: escape(JSON.stringify(zipData)) },
function (data, status, response) {
var filename = "";
var disposition = response.getResponseHeader('Content-Disposition');
if (disposition && disposition.indexOf('attachment') !== -1) {
var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
var matches = filenameRegex.exec(disposition);
if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
}
var type = response.getResponseHeader('Content-Type');
var blob = new Blob([data], { type: type });
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(blob, filename);
} else {
var URL = window.URL || window.webkitURL;
var downloadUrl = URL.createObjectURL(blob);
if (filename) {
var a = document.createElement("a");
if (typeof a.download === 'undefined') {
window.location = downloadUrl;
} else {
a.href = downloadUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
}
} else {
window.location = downloadUrl;
}
}
});
Using Service Call #2 the Zip File I get is corrupted 使用Service Call#2,我得到的Zip文件已损坏
Could it be the encoding? 可能是编码吗? I checked the data between the correct and incorrect Zip Files and they look like this: 我检查了正确和不正确的Zip文件之间的数据,它们看起来像这样:
PK ú‚ÐJmÇ´¸g € BOM.csvu'ËNÃ0?E÷HüÃÈ+ÆÑØy/›”WÔðH[Ä64IÕ¦|?>‰_ÀN(-¢l®,Ýã;wìÏ÷‡m^Ö³ú 35VkUŽtÕf6)óºZcZjqz"0?dÒ³ü9TÓ%yd#ˆ3Ö˜R›¡kÙMYæt?2'Òâ¦É½dÈhO¶"BXÁ?ùÚ”Ç<‰,ÖÍ '?ååÎé ÁÝ!Ò ²AˆVG ]3? PKú,ÐJmÇ'¸g€BOM.csvu'ËNÃ0?E÷HüÃ+ +ÆÑØy/>“WÔðH[Ä64IÕ| |?>‰_ÀN( - ¢l®,Ýã;wìÏ÷‡m ^³³ú35VkUŽtÕf6)óºZcZjqz “0?dÒ³9TÓ%yd#3Ö~R>¡kÙMYæt?2'Òâ|ɽdÈhO”BXÁ?ùÚ“Ç<‰,ÖÍ'?ååÎéÁ!Ò²AVG] 3?
PK ï¿¿ï¿¿ï¿¿g ï¿¿ BOM.csvuï¿¿ï¿¿ï¿¿?Eï¿¿ï¿¿+ï¿¿ï¿¿/ï¿¿ï¿¿Wï¿¿H[ï¿¿4IÕ¦|?>ï¿¿_ï¿¿(-ï¿¿lï¿¿,ï¿¿;wï¿¿ï¿¿ï¿¿m^ֳ￿kUï¿¿tï¿¿6)ï¿¿Zjqz"0?dҳ￿yd#ï¿¿3Ö˜Rï¿¿ï¿¿kï¿¿MYï¿¿2'￿￿ɽdï¿¿Oï¿¿"BXï¿¿ï¿¿ï¿¿,ï¿¿ ï¿¿?ï¿¿ï¿¿ï¿¿ï¿¿ï¿¿ï¿¿Aï¿¿VG ]3? PKï¿¿ï¿¿ï¿¿ïï¿¿BOM.csvuï¿¿ï¿¿ï¿¿?Eï¿¿ï¿¿+ï¿¿ï¿¿/ï¿¿ï¿¿Wï¿¿H [ï¿¿4IÕ||>ï¿¿_ï¿¿(-ï¿¿lï¿¿,ï¿¿;wï¿¿ï¿¿ï¿¿m^ֳ￿kUï¿¿tï¿¿ 6)ï¿¿Zjqz “0?dҳ￿yd#ï¿¿3Ö~Rï¿¿ï¿¿kï¿¿MYï¿¿2'￿￿ɽdï¿¿Oï¿¿” BXï¿¿ï ¿¿ï¿¿,ï¿¿ï¿¿?ï¿¿ï¿¿ï¿¿ï¿¿ï¿¿ï¿¿Aï¿¿VG] 3?
It seems that the files were encoded differently. 似乎文件的编码方式不同。 What do you guys think? 你们有什么感想?
Thank you Musa for giving me that clue. 谢谢Musa给了我那个线索。
Now here is the Service Call I am using to download my Zip File: 现在这是我用来下载我的Zip文件的服务调用:
var xhrq = new XMLHttpRequest();
xhrq.onreadystatechange = function () {
if (this.readyState == 4 ) {
if (this.status == 200) {
var xhrp = this;
var response = this.response;
// check for a filename
var filename = "";
var disposition = xhrp.getResponseHeader('Content-Disposition');
if (disposition && disposition.indexOf('attachment') !== -1) {
var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
var matches = filenameRegex.exec(disposition);
if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
}
var type = xhrp.getResponseHeader('Content-Type');
var blob = new Blob([response], { type: type, encoding: "UTF-8" });
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(blob, filename);
} else {
var URL = window.URL || window.webkitURL;
var downloadUrl = URL.createObjectURL(blob);
if (filename) {
var a = document.createElement("a");
if (typeof a.download === 'undefined') {
window.location = downloadUrl;
} else {
a.href = downloadUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
}
} else {
window.location = downloadUrl;
}
setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); // cleanup
}
} else {
// Call Back for Error
}
}
}
// Open HTTP Request and Call API Function
xhrq.open("POST", appPath + "DownloadZipFile");
xhrq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhrq.responseType = 'blob';
xhrq.send('designData=' + escape(JSON.stringify(zipData)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.