【问题标题】:Failed to download files when deployed to Azure App Service部署到 Azure 应用服务时下载文件失败
【发布时间】:2021-03-09 23:36:44
【问题描述】:

发现在 IIS 上运行的大文件下载代码。

但它停留在 Azure AppService。

文件会很好地下载,然后在达到 1 GB 时停止。

Azure 应用服务设置有问题吗?

请告诉我问题出在哪里。

这是ashx文件代码。

public void ProcessRequest(HttpContext context)
{
    Stream stream = null;
 
    int bytesToRead = 10000;

    byte[] buffer = new Byte[bytesToRead];
    
    string Url = context.Request.QueryString["Url"];
    string FileName = HttpUtility.UrlEncode(context.Request.QueryString["FileName"]).Replace("+","%20");
    try
    {
        HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create(Url);

        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=\"" + FileName + "\"");
        resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());

        int length;
        do
        {
            if (resp.IsClientConnected)
            {
                length = stream.Read(buffer, 0, bytesToRead);

                resp.OutputStream.Write(buffer, 0, length);

                resp.Flush();

                buffer = new Byte[bytesToRead];
            }
            else
            {
                // cancel the download if client has disconnected
                length = -1;
            }
        } while (length > 0); //Repeat until no data is read
    }
    finally
    {
        if (stream != null)
        {
            //Close the input stream
            stream.Close();
        }
    }
}

【问题讨论】:

    标签: c# asp.net azure azure-web-app-service


    【解决方案1】:

    老实说,我使用您的代码下载超过 1GB 的大文件并没有遇到任何问题,即使在发布到 Azure 之后也是如此。

    首先,httpWebRequest 没有任何人为的大小限制。我想知道您是否应该考虑下载其他代码,因为如果我们看不到有关错误日志的详细信息以及下载过程的详细信息,那很不方便。

    这里有一个问题可能会启发你:C# - Is there a limit to the size of an httpWebRequest stream?

    如果您想尝试其他代码,try this:

    static void Main(string[] args)
    {
        HttpWebRequestDownload hDownload = new HttpWebRequestDownload();
    
        string downloadUrl = "http://speedtest.tele2.net/10MB.zip";
        hDownload.DownloadProgressChanged += HDownloadOnDownloadProgressChanged;
        hDownload.DownloadFileCompleted += delegate(object o, EventArgs args)
        {
            Debug.WriteLine("Download finished and saved to: "+hDownload.downloadedFilePath); 
        };
        hDownload.Error += delegate(object o, string errMessage) { Debug.WriteLine("Error has occured !! => "+errMessage); };
        hDownload.DownloadFile(downloadUrl);
    }
    
    
    private void HDownloadOnDownloadProgressChanged(object sender, HttpWebRequestDownload.ProgressEventArgs e)
    {
        Debug.WriteLine("progress: "+e.TransferredBytes+" => "+e.TransferredPercents);
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 2020-05-18
      • 1970-01-01
      • 2021-07-10
      • 1970-01-01
      • 2020-09-09
      • 2018-11-11
      • 2014-01-09
      • 2022-06-16
      相关资源
      最近更新 更多