ajax returns 错误 404/上传多个文件时找不到控制器 ASP.net
ajax returns error 404/can't find controller when uploading multiple files ASP.net
我的ajax上传多个大文件时好像找不到控制器
我的看法:
<input type="file" id="examen_ingresar" name="examen_ingresar" multiple>
我的 Js:
$.ajax({
type: "POST",
url: '/Analisis/IngresarExamen?id_analisis=' + id_analisis ,
contentType: false,
processData: false,
data: data,
success: function (result) {
retorno = result;
},
error: function (xhr, status, p3, p4) {
var err = "Error " + " " + status + " " + p3 + " " + p4;
if (xhr.responseText && xhr.responseText[0] == "{")
err = JSON.parse(xhr.responseText).Message;
console.log(err);
}
});
AnalisisController.cs:
[HttpPost]
public JsonResult IngresarExamen(long id_analisis)
{
int retorno = 0;
try
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFileBase file = Request.Files[i];
string direccion = Path.Combine(Server.MapPath("/AppUploads/" + id_analisis), Guid.NewGuid() + Path.GetExtension(file.FileName));
System.IO.Directory.CreateDirectory(Server.MapPath("/AppUploads/"+ id_analisis));
retorno += amodel.IngresarExamen(id_analisis, direccion);//Just a method that make the register, if it's ok returns 0
System.IO.Stream fileContent = file.InputStream;
file.SaveAs(Path.Combine(direccion));
}
}
catch (Exception)
{
retorno++;
Response.StatusCode = (int)HttpStatusCode.BadRequest;
}
return Json(retorno);
}
我还将这些行添加到 Web.config 因为可能是按文件大小(允许上传 5gb)
<system.web>
<httpRuntime targetFramework="4.6.1" maxRequestLength="5242880" executionTimeout="2400" />
<compilation debug="true" targetFramework="4.6.1"/>
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="5242880"/>
</requestFiltering>
</security>
</system.webServer>
这实际上适用于一些小文件,所以我用 3 个大小为 1gb 的文件对其进行了测试,大约 5 秒后它显示在浏览器控制台中:
POST http://localhost:60139/Analisis/IngresarExamen?id_analisis=40 404 (Not Found)
在搜索这个之后我意识到我的错误并且实际上在 Web.config
<system.web>
<httpRuntime targetFramework="4.6.1" maxRequestLength="5242880" executionTimeout="2400" />
<compilation debug="true" targetFramework="4.6.1"/>
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="5242880"/>
</requestFiltering>
</security>
</system.webServer>
->maxRequestLength is in kb
->executionTimeout is in seconds
->maxAllowedContentLength is in BYTES
因此,如果您遇到同样的问题,请尝试将这些值更改为您需要的值
另外 maxAllowedContentLength 的最大值大约为 4gb,除非您使用其他方法上传文件
我的ajax上传多个大文件时好像找不到控制器
我的看法:
<input type="file" id="examen_ingresar" name="examen_ingresar" multiple>
我的 Js:
$.ajax({
type: "POST",
url: '/Analisis/IngresarExamen?id_analisis=' + id_analisis ,
contentType: false,
processData: false,
data: data,
success: function (result) {
retorno = result;
},
error: function (xhr, status, p3, p4) {
var err = "Error " + " " + status + " " + p3 + " " + p4;
if (xhr.responseText && xhr.responseText[0] == "{")
err = JSON.parse(xhr.responseText).Message;
console.log(err);
}
});
AnalisisController.cs:
[HttpPost]
public JsonResult IngresarExamen(long id_analisis)
{
int retorno = 0;
try
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFileBase file = Request.Files[i];
string direccion = Path.Combine(Server.MapPath("/AppUploads/" + id_analisis), Guid.NewGuid() + Path.GetExtension(file.FileName));
System.IO.Directory.CreateDirectory(Server.MapPath("/AppUploads/"+ id_analisis));
retorno += amodel.IngresarExamen(id_analisis, direccion);//Just a method that make the register, if it's ok returns 0
System.IO.Stream fileContent = file.InputStream;
file.SaveAs(Path.Combine(direccion));
}
}
catch (Exception)
{
retorno++;
Response.StatusCode = (int)HttpStatusCode.BadRequest;
}
return Json(retorno);
}
我还将这些行添加到 Web.config 因为可能是按文件大小(允许上传 5gb)
<system.web>
<httpRuntime targetFramework="4.6.1" maxRequestLength="5242880" executionTimeout="2400" />
<compilation debug="true" targetFramework="4.6.1"/>
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="5242880"/>
</requestFiltering>
</security>
</system.webServer>
这实际上适用于一些小文件,所以我用 3 个大小为 1gb 的文件对其进行了测试,大约 5 秒后它显示在浏览器控制台中:
POST http://localhost:60139/Analisis/IngresarExamen?id_analisis=40 404 (Not Found)
在搜索这个之后我意识到我的错误并且实际上在 Web.config
<system.web>
<httpRuntime targetFramework="4.6.1" maxRequestLength="5242880" executionTimeout="2400" />
<compilation debug="true" targetFramework="4.6.1"/>
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="5242880"/>
</requestFiltering>
</security>
</system.webServer>
->maxRequestLength is in kb
->executionTimeout is in seconds
->maxAllowedContentLength is in BYTES
因此,如果您遇到同样的问题,请尝试将这些值更改为您需要的值 另外 maxAllowedContentLength 的最大值大约为 4gb,除非您使用其他方法上传文件