无法将超过4MB的文件发布到Web API [英] Not able to post more than 4MB files to Web API

查看:136
本文介绍了无法将超过4MB的文件发布到Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在向我的Web API发布超过4MB的内容时,它会抛出404(未找到)错误,否则如果正在使用([EnableCors(origins:" http://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: "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");
                }
            });
        }
    });

});

推荐答案

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

Asp.net 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆