部署到Azure应用程序服务时无法下载文件



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

但它仅限于Azure应用服务。

该文件下载良好,然后在达到1GB时停止。

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();
}
}
}

老实说,即使在发布到Azure之后,我也不会在使用您的代码下载超过1GB的大型文件时遇到任何问题。

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

这里有一个问题可能会启发你:C#-httpWebRequest流的大小有限制吗?

如果你想尝试另一个代码,试试这个:

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);
}

最新更新