C# 在OSX上下载文件时出现网络错误
我有一个带有照片列表的网站。用户可以选择下载每张照片。下载只是将结果写入输出流 代码如下:C# 在OSX上下载文件时出现网络错误,c#,asp.net,macos,C#,Asp.net,Macos,我有一个带有照片列表的网站。用户可以选择下载每张照片。下载只是将结果写入输出流 代码如下: [WebMethod] public static void DownloadPhotoAsset(string assetId) { var photoAsset = GetPhotoAsset(assetId); Stream stream = null; int bytesToRead = 10000; byte[] buffer = new Byte[bytesT
[WebMethod]
public static void DownloadPhotoAsset(string assetId)
{
var photoAsset = GetPhotoAsset(assetId);
Stream stream = null;
int bytesToRead = 10000;
byte[] buffer = new Byte[bytesToRead];
try
{
HttpWebRequest fileReq =
(HttpWebRequest)HttpWebRequest.Create(photoAsset.FileAbsoluteUrl);
HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();
if (fileReq.ContentLength > 0)
fileResp.ContentLength = fileReq.ContentLength;
stream = fileResp.GetResponseStream();
var resp = HttpContext.Current.Response;
resp.ContentType = "application/octet-stream";
resp.AddHeader("Content-Disposition",
"attachment; filename=\"" +
Path.GetFileName(photoAsset.FileAbsoluteUrl) + "\"");
resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());
int length;
do
{
// verify that the client is connected.
if (resp.IsClientConnected)
{
// read data into the buffer and write it out
// to the response's output stream
length = stream.Read(buffer, 0, bytesToRead);
resp.OutputStream.Write(buffer, 0, length);
// flush the data and clear the buffer
resp.Flush();
buffer = new Byte[bytesToRead];
}
else
length = -1; // cancel the download if client has disconnected
} while (length > 0); //Repeat until no data is read
}
finally
{
if (stream != null)
stream.Close(); // close the input stream
}
}
这在Windows上的每个浏览器中都可以正常工作,但我在Mac上遇到网络连接问题
- 在Safari中,下载在一秒钟后停止,并显示“网络连接丢失”
- 在Chrome中,错误显示为“失败-网络错误”
- 在Firefox中,错误显示“下载错误-image.jpeg.part无法保存,因为无法读取源文件”
有人知道我做错了什么吗?Mac电脑在不同的网络上吗?@DanielA.White-是的。我也在不同网络上的Windows机器上进行了测试,下载效果很好。您是否在与macs相同的网络上测试了Windows机器?@DanielA.White-是的,它们工作正常。