gpt4 book ai didi

javascript - 通过 jQuery AJAX 上传文件后 HttpPostedFile.ContentLength 始终为 -2

转载 作者:行者123 更新时间:2023-12-01 01:42:03 29 4
gpt4 key购买 nike

我对 jQuery AJAX 和 ASSX 行为有奇怪的问题。这是我的代码:

<input type="file"  ID="FileUpload1"  multiple="multiple" />
<input type="button" ID="Button1" Text="Upload Selected File(s)" />
function Upload() {
var data = new FormData();
jQuery.each($('#FileUpload1')[0].files, function (i, file) {
data.append('file-' + i, file);
});

$.ajax({
url: "/basic/fileupload/FileUploadHandler.ashx",
type: "POST",
contentType: false,
processData: false,
cache: false,
async: true,
data: data,
error: function (data) {
alert("Erro no envio de fotos do projecto. " + data.status);
}
});
}
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
try
{
string dirFullPath = HttpContext.Current.Server.MapPath("~/MediaUploader/");
string[] files;
int numFiles;
files = System.IO.Directory.GetFiles(dirFullPath);
numFiles = files.Length;
numFiles = numFiles + 1;
string str_image = "";

foreach (string s in context.Request.Files)
{
HttpPostedFile file = context.Request.Files[s];
string fileName = file.FileName;
string fileExtension = file.ContentType;

if (!string.IsNullOrEmpty(fileName))
{
fileExtension = System.IO.Path.GetExtension(fileName);
str_image = "MyPHOTO_" + numFiles.ToString() + fileExtension;
string pathToSave_100 = HttpContext.Current.Server.MapPath("~/files/") + str_image;
file.SaveAs(pathToSave_100);
}
}
// database record update logic here ()

context.Response.Write(str_image);
}
catch (Exception ac)
{
}
}

一切看起来都很好,但结果是:

context.Request.Files[0]
{System.Web.HttpPostedFile}
ContentLength: -2
ContentType: "image/png"
FileName: "icon-large.png"
InputStream: {System.Web.HttpInputStream}

我可以接收文件名,但 ContentLength 始终为 -2,保存文件后大小仅为 0 字节。您能帮我解决这个问题吗?

更新:我发现了一些新东西,它可以在 ASP.net 开发服务器上正常工作(通过在 Visual Studio 中按 F5 键直接运行应用程序),但 IIS 8.5 配置出现问题

我的 web.config 请求长度参数是:

    <httpRuntime requestValidationMode="2.0" maxRequestLength="10240000" requestLengthDiskThreshold="10240000" />

更新2:将应用程序池的托管管道模式更改为经典可以解决问题,但我会失去 URL 重写,因此无法更改管道模式。有什么想法吗?

最佳答案

我已经找到解决这个问题的方法了。我不知道这是否好,但解决了我的问题:

我用过

void Application_BeginRequest(object sender, EventArgs e) 

global.asax

文件来管理请求,因为内容在那里正确可见。还有其他想法吗?

关于javascript - 通过 jQuery AJAX 上传文件后 HttpPostedFile.ContentLength 始终为 -2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38244943/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com