最近遇到的一個項目,附件在A服務器,對外是B服務器,A與B服務器可以內(nèi)網(wǎng)通信,但是A服務器不對外,就需要用到B服務器的對外訪問地址瀏覽附件與文件,參考了部分網(wǎng)上的代碼,附上代碼。
PS:不支持加速器下載
-
-
-
-
-
-
private async void FileDownloadAsync(string url)
-
-
-
await System.Threading.Tasks.Task.Run(async () =>
-
-
-
-
-
using (HttpClient http = new HttpClient())
-
-
var httpResponseMessage = await http.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
-
var contentLength = httpResponseMessage.Content.Headers.ContentLength;
-
string contentType = httpResponseMessage.Content.Headers.ContentType.MediaType??MimeMapping.GetMimeMapping(url);
-
string fileName = httpResponseMessage.Content.Headers.ContentDisposition!=null?httpResponseMessage.Content.Headers.ContentDisposition.FileName:Path.GetFileName(url);
-
-
-
fileName = Path.GetFileName(url);
-
-
using (var stream = await httpResponseMessage.Content.ReadAsStreamAsync())
-
-
-
byte[] bytes = new byte[readLength];
-
-
var beginSecond = DateTime.Now.Second;
-
-
-
Response.ContentType = "application/octet-stream";
-
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
-
-
Response.AddHeader("Content-Length", contentLength.HasValue?contentLength.Value.ToString():"0");
-
while ((writeLength = stream.Read(bytes, 0, readLength)) > 0)
-
-
-
if (Response.IsClientConnected)
-
-
Response.OutputStream.Write(bytes, 0, writeLength);
-
-
-
-
downloadSize += writeLength;
-
downloadSpeed += writeLength;
-
-
-
-
-
-
該文章在 2022/7/5 8:36:11 編輯過