2
Answers

Run out of memory, Run out Rensponse time

Photo of Nepethya Rana

Nepethya Rana

7y
170
1

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)

0
Photo of Mahesh Chand
2 286.9k 123.7m 18y
John,
Shoot me an email with your contact information to mahesh AT c-sharpcorner DOT com. I may be able to help you out.
0
Photo of milind khire
NA 33 0 18y
Dear Sir, We, Opel Instruments Pvt. Ltd. IT division are a fast growing company providing IT services over a wide spectrum of technologies. We have good experience in ASP.NET. We have done many projects of conversion and modification. Modifications will be performed as and when required with respect to agreed upon deliverables. We can host daily update for your convenience. Please contact me on opelind@pn3.vsnl.net.in /opelind@dataone.in or may contact on mobile to Mayuresh (Tech. Director )+91 9890129281 or Manoj (Director) +91 9370150520. Our portfolio of site is available for you review at: http://www.opel-instruments.com/ITDiv/index_it.htm I look forward to hearing from you. Thank you for your consideration. milind
0
Photo of Nis
NA 2k 0 18y

Trust will be developed overtime. Initially try us on a small pilot project, feel us and get your worth. Can we have a voice/chat?