Hi,
While working on a MergePDF application to do the following:
1. Upload two or more .pd files to the server
2. Concatenate file to generate new .pdf
3. Provide the merged file for download
One request works fine. When multiple requests are sent simultaneously to the shared Windows server, some of the requests fail. For example, I sent 16 simultaneous requests. One time, four of them failed. Another time, all succeeded. A third time, fourteen of sixteen failed! Error messages:
* Page not found
* Wrong format in true type font!
* Object reference not set to an instance of an object.
* Safe handle has been closed
* select/poll returned error
* transfer closed with outstanding read data remaining
Please help. Here is the code snippett
protected void Page_Load(object sender, EventArgs e)
{
//Adding a delay of 2 seconds
System.Threading.Thread.Sleep(2000);
Filename = Guid.NewGuid().ToString();
//LogEntry("Footer Text-" + Request.QueryString["text"].ToString());
string dirName = Guid.NewGuid().ToString();
Directory.CreateDirectory(Request.PhysicalApplicationPath + "..\\db\\TempPdf" + "\\" + dirName);
path = Request.PhysicalApplicationPath + "..\\db\\TempPdf" + "\\" + dirName;
LogEntry("1). Page Load");
string OutputFileName = Path.Combine(path, Guid.NewGuid().ToString() + ".pdf");
finalFileName = Path.Combine(path, Filename + ".pdf");
LogEntry("1). All fle names are declaired");
HttpFileCollection httpPostedFiles = Request.Files;
string[] TempFileName = new string[httpPostedFiles.Count];
Aspose.Pdf.Kit.License licencekit = new Aspose.Pdf.Kit.License();
licencekit.SetLicense(Request.PhysicalApplicationPath + "\\" + "Aspose.Total.lic");
LogEntry("First File Name");
LogEntry(httpPostedFiles[0].FileName);
LogEntry("Second File Name");
LogEntry(httpPostedFiles[1].FileName);
LogEntry("Both file names are written");
try
{
for (int Loop1 = 0; Loop1 < httpPostedFiles.Count; Loop1++)
{
// Create a new file name.
TempFileName[Loop1] = Path.Combine(path, Guid.NewGuid().ToString() + ".pdf");
// Save the file.
httpPostedFiles[Loop1].SaveAs(TempFileName[Loop1]);
}
LogEntry("2). The uploaded files are stored at server");
PdfFileEditor pdfEditor = new PdfFileEditor();
pdfEditor.Concatenate(TempFileName, OutputFileName);
LogEntry("3). Merged pdf is formed");
//Response.Clear();
//Response.ContentType = "application/pdf";
//Response.AddHeader("Content-Disposition", "attachment; filename=");
////Response.AddHeader("Content-Length", finalFileName.Length.ToString());
//Response.Flush();
//Response.Write(finalFileName);
//LogEntry("1). Before transmit file");
////Response.TransmitFile(finalFileName);
//LogEntry("1). After transmit file");
//Response.End();
}
catch (Exception ex)
{
string query = ex.Message.ToString();
//query = "Error Message";
Response.Redirect("ErrorPage.aspx?error='" + query + "'");
//Response.End();
//Server.Transfer("ErrorPage.aspx");
//LogEntry(ex.Message);
Label1.Text = ex.Message.ToString();
}
finally
{
//response to the client
//open the finalfile in filestream before delete it
FileStream responseFile = new FileStream(finalFileName, FileMode.Open);
byte[] buffer1 = new byte[Convert.ToInt64(responseFile.Length)];
Response.ContentType = "application/pdf";
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=");
Response.AddHeader("Content-Length", responseFile.Length.ToString());
//Response.AddHeader("Content-Length",fi.Length.ToString() );
Response.Charset = "UTF-8";
responseFile.Read(buffer1, 0, (int)responseFile.Length);
responseFile.Close();
Response.BinaryWrite(buffer1);
//Response.WriteFile(finalFileName);
//Directory.Delete(path);
string[] files = Directory.GetFiles(path);
foreach (string file in files)
File.Delete(file);
Directory.Delete(path);
Response.End();
}
}