[英]WCF Download File Aborted By Client
我有以下用於下載大文件的代碼。 它從文件流讀取並寫入Response.Outputstream。
它似乎正在運行,即文件似乎下載了(實際上,似乎很奇怪地下載了更多文件),但最終失敗了。 Chrome給出“網絡錯誤”,而IE顯示“(已中止)”
[OperationContract]
[WebInvoke(UriTemplate = "/f/{key}", Method = "GET")]
public void LargeFileDownload(string key)
{
var identifier = PublicIdentifier.FromString(key, true);
if (identifier.Type == PublicIdentifier.IdentifierType.DocumentDownload)
{
Document doc = Business.Documents.GetById(Application.SystemUser, identifier.Id);
string tempName = Path.GetTempPath() + doc.OriginalFileName;
int bufferSize = 8192;
FileStream fstream = new FileStream(tempName, FileMode.Open, FileAccess.Read);
long fileSize = fstream.Length;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = doc.ContentType;
string contentDisposition = string.Format("{0};filename={1}{2}", "attachment", doc.Name.Replace(" ", "_"), Path.GetExtension(doc.OriginalFileName));
HttpContext.Current.Response.AddHeader("Content-Disposition", contentDisposition);
HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
WebOperationContext.Current.OutgoingResponse.ContentType = doc.ContentType;
try
{
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
while ((bytesRead = fstream.Read(buffer, 0, bufferSize)) > 0)
{
HttpContext.Current.Response.OutputStream.Write(buffer, 0, bufferSize);
HttpContext.Current.Response.Flush();
}
}
finally
{
if (fstream != null)
fstream.Close();
//File.Delete(tempName);
}
}
}
更新的代碼:-
[OperationContract]
[WebInvoke(UriTemplate = "/f/{key}", Method = "GET")]
public void LargeFileDownload(string key)
{
var identifier = PublicIdentifier.FromString(key, true);
if (identifier.Type == PublicIdentifier.IdentifierType.DocumentDownload)
{
Document doc = Business.Documents.GetById(Application.SystemUser, identifier.Id);
string tempName = Path.GetTempPath() + doc.OriginalFileName;
int bufferSize = 8192;
FileStream fstream = new FileStream(tempName, FileMode.Open, FileAccess.Read);
long fileSize = fstream.Length;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "application/octet-stream";
string contentDisposition = string.Format("{0};filename={1}{2}", "attachment", doc.Name.Replace(" ", "_"), Path.GetExtension(doc.OriginalFileName));
HttpContext.Current.Response.AddHeader("Content-Disposition", contentDisposition);
HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
try
{
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
while ((bytesRead = fstream.Read(buffer, 0, bufferSize)) > 0)
{
HttpContext.Current.Response.OutputStream.Write(buffer, 0, bytesRead);
HttpContext.Current.Response.Flush();
}
}
finally
{
if (fstream != null)
fstream.Close();
//File.Delete(tempName);
}
}
}
提琴手原始:-
HTTP/1.1 504 Fiddler - Receive Failure
Date: Wed, 17 Oct 2018 11:53:09 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
Cache-Control: no-cache, must-revalidate
Timestamp: 12:53:09.099
[Fiddler] ReadResponse() failed: The server did not return a complete response for this request. Server returned 13,912,938 bytes.
您正確設置了Content-Length
,瀏覽器假定該值為true。 但是由於這個錯誤,您正在發送更多數據(這是垃圾):
HttpContext.Current.Response.OutputStream.Write(buffer, 0, bufferSize);
這不應該是bufferSize
而應該是bytesRead
。 這導致協議錯誤,導致瀏覽器中止其處理。
復制循環可能應該由對TransmitFile
的調用或至少由Stream.Copy
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.