问题
While am posting more than 4MB to my Web API it is throwing error as 404(Not Found) or else if am using ([EnableCors(origins: "http://localhost:61365", headers: "", methods: "")]) cors request it is throwing the error like cross origin domain error. Below is my code
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");
}
});
}
});
});
回答1:
Asp.net has a limit on max request length. Have you checked that? Increase the param value
<system.web>
<httpRuntime maxRequestLength="30096" />
</system.web>
回答2:
You need to look for the following 2 settings in Web.config to increase the upload size:
maxRequestLength="size in kbytes"
<system.web>
<httpRuntime targetFramework="4.5" maxQueryStringLength="" maxRequestLength="" maxUrlLength="" />
maxAllowedContentLength is in bytes
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="" maxQueryString="" maxUrl=""/>
来源:https://stackoverflow.com/questions/39630359/not-able-to-post-more-than-4mb-files-to-web-api