0

在调用 PageLoad 下载方法上获取查询字符串参数后,我在查询中提供了下载 Iframe 和输入参数的源。在下载方法中,调用 wcf 服务以从字节数组中的 sql DB 获取文件数据,如果它是大文件,则将其写入临时位置。我的下载工作正常,但对于大文件,我的 Web 应用程序在文件下载完成之前没有响应。我尝试了很多东西,比如线程、异步任务和等待,但仍然是同样的问题。主页中的代码是

using System.Threading;
protected void Page_Load(object sender, EventArgs e)
{   
    string eVENTTARGET = Request[Constants.EVENTTARGET];       
     if (EventTarget.Contains("FileDownload"))
         {
            string[] argumenstsArr = eVENTARGUMENT.Split('_');
            string qryString = "objectRid=" + argumenstsArr[0];
            new Thread(() =>
            {
                            Thread.CurrentThread.IsBackground = true;
                            GiveSourceToDownloadIframe(qryString);
                        }).Start();
            }
}
private void GiveSourceToDownloadIframe(string qryString)
{
            DownloadFrame.Src = "/View/Pages/DocumentManagement/ActualDownload.aspx?" + qryString;
}

我在 iframe 源页面上的下载方法如下。

protected void DownloadFile(long objectRidToDownloadFile, string clientName, bool isDownloadForCustomField)
        {
            objDocEntity = new DocumentManagementEntity
            {
                ObjEntity = new ObjectEntity(),
                ObjRid = objectRidToDownloadFile,
                UsrRid = usrRid,
                IsCustomFieldDownload = isDownloadForCustomField
            };
            string isDownloadFileWithFileNameOnly = string.Empty;
            try
            {

                AwarebaseExceptionEntity exceptionEntity;
                DocumentManagementEntity documentHandlerEntity;
                using (DocumentManagementServiceClient documentManagementServiceClient = new DocumentManagementServiceClient())
                {
            // wcf calling to get file bytearray fatching from sql db in case of small file and save it to temp location first in case of large file.
                    documentHandlerEntity = documentManagementServiceClient.ProcessExportRequest(objDocEntity, clientName, out exceptionEntity);
                }

                if (documentHandlerEntity != null)
                {
                    
                    if (documentHandlerEntity.FileSize > 0 && documentHandlerEntity.IsFileAlreadyCopiedinTempFolder == false && documentHandlerEntity.TempDownloadLocation != null)
                    {
                        byte[] byteArrayFile = File.ReadAllBytes(documentHandlerEntity.TempDownloadLocation);
                        documentHandlerEntity.FileData = byteArrayFile;
                    }
                    if (documentHandlerEntity.FileData != null || documentHandlerEntity.IsFileAlreadyCopiedinTempFolder == true)
                    {
                        //Check if file extension has . or not
                        if (!documentHandlerEntity.FileExtension.Contains('.'))
                        {
                            documentHandlerEntity.FileExtension = string.Format(CultureInfo.CurrentCulture, ".{0}", documentHandlerEntity.FileExtension);
                        }

                        string fullFileName;

                        if (!string.IsNullOrEmpty(isDownloadFileWithFileNameOnly) && isDownloadFileWithFileNameOnly.ToLower(CultureInfo.CurrentCulture) == "true")
                        {
                            if (!string.IsNullOrEmpty(documentHandlerEntity.ObjEntity.Title1))
                            {
                                fullFileName = documentHandlerEntity.ObjEntity.Title1;
                            }
                            else
                            {
                                string msg = Helper.GetKeyValue(Constants.CustomFieldNameFileDownloadMsg, Helper.GetClient());
                                ScriptManager.RegisterClientScriptBlock(Page, GetType(), Constants.RegisterClientScriptBlockMethod, $"window.parent.showWarningMessage(\"{msg}\");", true);
                                return;
                            }
                        }
                        else
                        {
                            fullFileName = $"{documentHandlerEntity.ObjEntity.IdNumber}_{documentHandlerEntity.ObjEntity.VersionNumber}_{documentHandlerEntity.ObjEntity.ObjectRid}_{documentHandlerEntity.ObjEntity.Title1}";
                        }
                        fullFileName += documentHandlerEntity.FileExtension;
                        Response.Clear();
                        Response.AddHeader(Constants.ContentDisposition, $"attachment;filename=\"{fullFileName}\"");
                        Response.AddHeader(Constants.ContentLength, documentHandlerEntity.FileSizeString);
                        Response.ContentType = Constants.applicationoctetstream;
                        Response.TransmitFile(documentHandlerEntity.FileName);
                        Response.Flush();
                        Response.Close();
                    }
                    
                }
               
            }
            catch (Exception exception)
            {
               //some exception handling code here
            }
        }

我想要一些建议或代码提示,以便我可以独立下载而不会冻结我的 Web 应用程序。提前致谢。

4

0 回答 0