I have an ASP.NET application that runs on:
Windows Server 2019 Standard
IIS Version 10.0.17763.1
Application is set up to use HTTPS
Application is using the library System.Web.UI.WebControls.FileUpload for file upload
Application is using QuickPDFDLL0811.dll to read PDF files and count the number of pages
Users have been reporting that SOME PDF files are failing to upload. The chrome browser tab shows a spinner as if the application is doing something and then the users get presented with a "The site can't be reached" page (attached). However, the same users have indicated they have taken the same PDF files that are erroring out through the steps I found on google here, and then they are able to upload the files successfully.
In addition, I took the same file that failed to upload in production and:
- Uploaded in the server where the PROD app is running through HTTP and chrome and it succeeded.
- Uploaded in our test environments using chrome and it via HTTP and HTTPS and it succeeded.
I am unable to share the files, as they contain sensitive data.
Has anyone run into a similar issue? or could anyone offer any guidance? The fact that I found a URL from the company docusign.com indicates to me that it is a common issue.
EDIT: I am adding the code that gets executed when the issue is triggered (BtnUploadClick). I forgot to mention that I also reviewed the IIS log files, but nothing is being logged with an unsuccessful response.
(My apologies for the weird formatting, Stackoverflow messes it up when I copy/paste from VSCode)
protected void BtnUploadClick(object sender, EventArgs e)
{
try
{
if (FileUpload.HasFiles)
{
foreach (var FileUpload in FileUpload.PostedFiles)
{
var uploadResult = Uploadfile(lstPrn, FileUpload);
if (uploadResult.ReturnResponse ==
Enumerators.ResponseEnum.Success)
{
var objPrintFile = uploadResult.ReturnObject;
lstPrn.Add(objPrintFile.FileName.ToUpper(),
objPrintFile);
}
}
}
else
{
lblFileCheck.Text = "You have not specified a file.";
lblFileCheck.ForeColor = Color.Red;
}
}
catch (Exception exception)
{
//Log exception
}
}
private Response<PrintFile> Uploadfile(Hashtable lstPrn,
HttpPostedFile FileUpload)
{
var result = new Response<PrintFile>();
var msg = "";
try
{
if (Path.GetExtension(fileToWork).ToLower() == ".pdf")
{
PdfUtil myPdfUtil = new PdfUtil();
objPrintFile.FileName = fileToWork;
objPrintFile.ExpectedDocs =
myPdfUtil.GetNumberOfPdfPages(path + fileToWork);
if (objPrintFile.ExpectedDocs > 0)
{
//Map object here
try
{
Session.Add("NewRequestPrintFiles", lstPrn);
result.ReturnResponse =
Enumerators.ResponseEnum.Success;
result.ReturnObject = objPrintFile;
btnNextUpload.Enabled = true;
}
catch
{
//log message here
}
}
}
}
catch (Exception ex)
{
//Log error
}
return result;
}
public int GetNumberOfPdfPages(string fileName)
{
int totPages = 0;
if (!string.IsNullOrEmpty(fileName))
{
using (StreamReader sr = new
StreamReader(File.OpenRead(fileName)))
{
Regex regex = new Regex(@"/Type\s*/Page[^s]");
int bufSize = 10 * 1024;
int lastPorSize = 20;
char[] myBuffer = new char[bufSize + 1];
char[] lastPortion = new char[lastPorSize];
for (int c = 0; c < lastPorSize; c++)
lastPortion[c] = '*';
try
{
//read it by chunks!!
while (!sr.EndOfStream)
{
int nBytes = sr.ReadBlock(myBuffer, 0, bufSize);
string strToSerach = new string(lastPortion, 0,
lastPorSize) + new string(myBuffer, 0, nBytes);
MatchCollection matches =
regex.Matches(strToSerach);
foreach (Match match in matches)
{
if (match.Index >= nBytes)
{
// I have to clean up if there is a match
// at the end of strToSerach because I
//don't want that to be copied over
// lastPortion and count again on the
//next iteration.
for (int c = 0; c < match.Length; c++)
{
if (match.Index + c > lastPorSize)
myBuffer[match.Index + c -
lastPorSize] = '*';
}
}
}
for (int c = 0; c < lastPorSize; c++)
{
if ((nBytes - lastPorSize + c) >= 0)
{
lastPortion[c] = myBuffer[nBytes -
lastPorSize + c];
}
}
totPages += matches.Count;
}
}
catch (Exception ex)
{
totPages = 0;
}
}
}
else
totPages = 0;
return totPages;
}
EDIT:
I am attaching a screenshot of the files. File1 is the same as File2, but notice there is a size difference when File1 is taken through the process of "Print to PDF" and then is saved locally. File1 fails to upload while File2 uploads successfully.