Nepethya Rana

Nepethya Rana

  • NA
  • 335
  • 145.9k

Run out of memory, Run out Rensponse time

Nov 3 2017 9:40 AM

How do I download Excel file without creating file in server ? size of file is 1000 kb. if multiple users tries to download , the file current process ran out of memory while creating files before downloading. What is the best way to handle the situation? Please help me.
My current Static Web method get called via JavaScript which then query database and creates excel files ( used open xml) with timestamp for every new call and saves in file server , and then it get invoked to download and later files gets deleted from the server.
issues I ran into : i) out of memory exception if many users tries to download the file. ii) sometimes error message "Someone else is working in \Filepath\file.xlsx right now . Please try again later." later pops up.
Could you please guide me?
my code :
java script (angular)
$http({
method: "POST",
url: "MyWebForm1.aspx/ExportExcelFile",
data: {
fileName: xlFile, dateString: dateString, otherParameter : parameters
},
contentType: "application/json",
dataType: "json"
}).then(function (response) {
var responsedata = JSON.parse(response.data.d);
if (responsedata.stat == 4) {
window.location.href = responsedata.redirectUrl;
}
else if (responsedata.stat == 2) {
$scope.onSuccessReturn = false;
$scope.onErrorReturn = true;
$scope.errorMessage = responsedata.message;
}
else {
window.location.href = responsedata.redirectUrl;
}
});
};
Code behind File (Asp.Net web form)
[WebMethod]
public static string ExportExcelFile(string fileName, string dateString, string otherParameter)
{
// Query Db and gets datatable
DataSet ds = new DataSet();
ds.Tables.Add(dt);
string xlfileName = fileName + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xlsx";
string filePath = fileLocation + xlfileName;
// This method created excel file using OPEN XML
DataSetToExcel dxl = new DataSetToExcel();
dxl.ExportDataSet(filePath, ds);
string urlToInvoke = "~/DownloadForm.aspx?fn=" + xlfileName;
return JsonConvert.SerializeObject(new { Success = true, redirectUrl = VirtualPathUtility.ToAbsolute(urlToInvoke) });
}
Code in download form:
protected void Page_Load(object sender, EventArgs e)
{
string fileName = Request.QueryString["fn"];
string filePath = ConfigurationManager.AppSettings["ExcelFilePath"] + fileName;
Response.ContentType = ContentType;
Response.AppendHeader("content-disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.TransmitFile(filePath);
Response.Flush();
System.Threading.Thread.Sleep(1000);
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
Response.End();
}

Answers (2)