I gave source to download Iframe with input parameter in querying over there after fetching the query string parameter on PageLoad Download method is called. In download Method a wcf service is called to fetch the data of file from sql DB in byte array, if it is large file then it is being written at a temp location instead. My download is working perfectly fine but for large file my Web application is unresponsive until file download is complete. I tried so many things like thread, Task with async and await but still same problem. Code in Main Page is

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

My download Method on iframe source page is below.

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

I want some suggestion or code hint so that I can do download independently without freezing my web application. Thanks in Advance.

0

There are 0 answers