当我向我的Web API发布超过4MB时,它会将错误抛出为404(未找到),否则如果正在使用([EnableCors(起源:" http://localhost:61365",标题: " ",方法:" ")])cors请求它抛出错误,如跨域错误。以下是我的代码
public class UploaderController : ApiController
{
[HttpPost]
public void UploadFile()
{
if (HttpContext.Current.Request.Files.AllKeys.Any())
{
// Get the uploaded image from the Files collection
var httpPostedFile = HttpContext.Current.Request.Files["UploadedImage"];
if (httpPostedFile != null)
{
// Validate the uploaded image(optional)
// Get the complete file path
var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/UploadedFiles"), httpPostedFile.FileName);
// Save the uploaded file to "UploadedFiles" folder
httpPostedFile.SaveAs(fileSavePath);
}
}
}
}
**WebAPPCode**
<html>
<head>
<title>Web API Samples</title>
<script src="../scripts/jquery-2.1.4.js"></script>
<script src="../scripts/scripts.js"></script>
<meta charset="utf-8" />
</head>
<body>
<form>
<span>Select file(s) to upload :</span>
<input id="file1" name="file1" type="file" multiple="multiple" />
<br />
<input id="btnSubmit" type="button" value="Upload" />
</form>
</body>
</html>
$(document).ready(function () {
$("#btnSubmit").click(function (evt) {
var files = $("#file1").get(0).files;
if (files.length > 0) {
var data = new FormData();
for (fileCount = 0; fileCount < files.length; fileCount++) {
data.append("file" + fileCount, files[fileCount]);
}
data.append("ID", "Mugesh");
$.ajax({
url: "http://localhost:8089/api/documentSynchronous/fileInsertUpload",
type: "POST",
contentType: false,
processData: false,
data: data,
success: function (messages) {
console.log(messages);
},
error: function (err) {
console.log("Error while invoking the Web API");
}
});
}
});
});
答案 0 :(得分:1)
您需要在Web.config中查找以下2个设置以增加上传大小:
maxRequestLength =&#34;大小以千字节为单位&#34;
<system.web>
<httpRuntime targetFramework="4.5" maxQueryStringLength="" maxRequestLength="" maxUrlLength="" />
maxAllowedContentLength以字节为单位
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="" maxQueryString="" maxUrl=""/>
答案 1 :(得分:0)
Asp.net对最大请求长度有限制。你检查过了吗?增加参数值
<system.web>
<httpRuntime maxRequestLength="30096" />
</system.web>