在OSX上下载文件时出现网络错误

时间:2013-05-16 19:15:16

标签: 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[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,因为无法读取源文件”

我已经使用OSX 10.7.4和OSX 10.8.3

检查了两台不同的Mac

任何人都知道我在这里做错了吗?

0 个答案:

没有答案