【发布时间】:2022-01-14 21:46:46
【问题描述】:
我有一个 ASP.NET 应用程序运行在:
Windows Server 2019 标准版
IIS 版本 10.0.17763.1
应用程序设置为使用 HTTPS
应用程序正在使用库 System.Web.UI.WebControls.FileUpload 进行文件上传
应用程序正在使用 QuickPDFDLL0811.dll 读取 PDF 文件并计算页数
用户一直报告某些 PDF 文件无法上传。 chrome 浏览器选项卡显示一个微调器,好像应用程序正在做某事,然后用户会看到“无法访问该站点”页面(附加)。但是,同样的用户表示他们已经通过我在 google here 上找到的步骤获取了错误的相同 PDF 文件,然后他们能够成功上传文件。
另外,我拿了在生产中上传失败的同一个文件:
- 通过 HTTP 和 chrome 上传到 PROD 应用运行的服务器中,并成功。
- 在我们的测试环境中使用 chrome 并通过 HTTP 和 HTTPS 上传并成功。
我无法共享这些文件,因为它们包含敏感数据。
有没有人遇到过类似的问题?或者有人可以提供任何指导吗?我从公司 docusign.com 中找到了一个 URL,这一事实向我表明这是一个常见问题。
编辑: 我正在添加触发问题时执行的代码(BtnUploadClick)。我忘了提及我还查看了 IIS 日志文件,但没有记录任何响应不成功的内容。
(我为奇怪的格式道歉,当我从 VSCode 复制/粘贴时,Stackoverflow 搞砸了)
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;
}
编辑:
我附上文件的屏幕截图。 File1 与 File2 相同,但请注意 File1 通过“打印为 PDF”的过程获取然后保存到本地时存在大小差异。 File1 上传失败,而 File2 上传成功。
【问题讨论】:
-
@Austin 我想我的意思是说 ASP.NET,因为文件扩展名以 .aspx 结尾,所以我附上了代码
-
@Austin 没有任何块触发,似乎调用甚至没有到达服务器,因为我无法在 IIS 日志中找到它。