【问题标题】:upload mp4 via ajax to mvc controller failing通过ajax上传mp4到mvc控制器失败
【发布时间】:2017-10-12 02:37:03
【问题描述】:

我正在将 mp4 上传到我的 mvc 控制器,但 Ajax 返回此错误:“Error error Not Found undefined” 我的看法

<form id="CrearRecurso">
<div class="container">      
    <div class="form-group">
        @Html.LabelFor(m => m.Imagen)
        @Html.TextBoxFor(m => m.Imagen, new { type = "file", name = "Imagen" })
        @Html.ValidationMessageFor(m => m.Imagen)
    </div>
    <button id="submiter" type="submit" class="btn btn-primary">Crear recurso!</button>
</div>

这是我的 ajax 部分,我在这里调用我的 API。但是我收到一个错误,您将在代码之后看到下面的错误。

$('#CrearRecurso').submit(function (e) {              
            var formData = new FormData($('#CrearRecurso').get(0));
            jQuery.support.cors = true;
            var files = $("#Imagen").get(0).files;
            var name = files.name;
            if (files.length > 0) { //checks if there is a file 

                if (window.FormData !== undefined) {
                    $.ajax({
                        type: "post",
                        url: '/api/mycontroller/Create?MyId=' + LeccionId,
                        contentType: false,
                        processData: false,
                        data: formData,
                        success: function (result) {
                            //do something 
                          
                        },
                    error: function (ts) { alert(ts.responseText) }
                    });
                }
                }
            }
        });
    });

当我上传 pdf 或 jpg 时没问题,但是当我使用 mp4 文件时,我在警报中收到此错误。

文件上传相关的模型如下:

 [Required]
    [DataType(DataType.Upload)]
    public HttpPostedFileBase Imagen { get; set; }

为什么会这样?谢谢。

更新 1:我的文件大小约为 50mb。会不会和问题有关?

【问题讨论】:

  • 首先,您没有取消默认提交,因此您对生成视图的方法以及您的 ajax 调用进行了 GET 操作
  • '/api/mycontroller/Create?MyId=' + LeccionId => 从您的错误中它没有达到该操作方法,那是 LeccionId 空还是空?出现弹出窗口是因为您的 AJAX 调用返回错误响应并点击 alert(ts.responseText)
  • Tetsuya,实际上当我使用 pdf 或 jpg 时它调用 api 没有问题,但是对于 mp4 我得到了那个错误

标签: jquery ajax asp.net-mvc html video


【解决方案1】:

在这种情况下,我可以看到我要上传的文件长度大于 4mb,我需要做的是更改 web.config 以允许 api 请求。

请参考这个答案:Dealing with large files in asp.Net API

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多