0

Large file download code found running on IIS.

But it stops at Azure AppService.

The file will download well and then stop when it reaches 1 GB.

Is there a problem with Azure App Service settings?

Please let me know what the problem is.

This is the ashx file code.

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();
        }
    }
}
bhl
  • 3
  • 1

1 Answers1

0

To be honest, I didn't get any trouble using your code to download large files more than 1GB, even after publishing to Azure.

First, httpWebRequest doesn't not has any artificial size limit. I was wondering if you should consider other code to download, because it's not convenient if we couldn't see the details about the error log, and the download process.

Here is a issue might inspire you: C# - Is there a limit to the size of an httpWebRequest stream?

If you want try another code, 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);
}
Doris Lv
  • 3,083
  • 1
  • 5
  • 14