无法将超过4MB的文件发布到Web API

编程入门 行业动态 更新时间:2024-10-25 12:23:38
本文介绍了无法将超过4MB的文件发布到Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在向我的Web API发布超过4MB的内容时,它会抛出404(未找到)错误,否则如果正在使用([EnableCors(origins:" localhost:61365 ,标头:" ,方法:" )))cors请求它抛出诸如跨源域错误之类的错误.以下是我的代码

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: "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: "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"); } }); } }); });

推荐答案

Asp对最大请求长度有限制.你检查了吗?增加参数值

Asp has a limit on max request length. Have you checked that? Increase the param value

<system.web> <httpRuntime maxRequestLength="30096" /> </system.web>

更多推荐

无法将超过4MB的文件发布到Web API

本文发布于:2023-10-12 01:22:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483356.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   MB   API   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!