【问题标题】:uploading pdf file using jquery mvc 4使用jquery mvc 4上传pdf文件
【发布时间】:2020-03-10 10:07:07
【问题描述】:

我正在尝试使用 jquery 上传文件,它适用于文本文件,但是当我尝试上传 pdf 文件时,它会出错并且总是将 0 返回到 Request.Files.Count。

实际上我正在尝试在上传之前预览文档,所以首先我在预览文件夹中上传一个文档,然后我使用 pdf 查看器将其显示在 div 中,如果他选择另一个文档,我将删除最后一个文档并上传新文档但我的问题是我的代码只适用于 .txt 文件,我想要 pdf

我尝试了 2 小时,但不明白错误,请帮助!

客户端代码

HTML

<input id="file" class="form-control-file" type="file" name="file" placeholder="Document Upload" /> 

jQuery

    $("#file").change(function () {
        var formData = new FormData();
        var totalFiles = document.getElementById("file").files.length;
        for (var i = 0; i < totalFiles; i++) {
            var file = document.getElementById("file").files[i];

            formData.append("file", file);
        }
        $.ajax({
            type: "POST",
            url: '/Admin/PreviewUpload',
            data: formData,
            dataType: 'json',
            contentType: false,
            processData: false,
            success: function (response) {
                alert('succes!!');
            },
            error: function (error) {
                alert("errror");
            }
        });
    });

服务器端代码

  [HttpPost]   
  public ActionResult PreviewUpload()
    {
        if (Request.Files.Count > 0)
        {
            foreach (string files in Request.Files)
            {
                var _file = Request.Files[files];
                FileInfo Fi = new FileInfo(Path.GetFileName(_file.FileName));
                string fileExtention = Fi.Extension;
                if (_file != null)
                {
                    if (fileExtention == ".PDF")
                    {
                        string fileName = Path.GetFileName(_file.FileName);
                        if (_file.ContentLength <= 120000000)
                        {
                            _file.SaveAs(Server.MapPath("~/PreviewPDF/" + fileName));
                        }
                        string path = "/PreviewPDF/" + Path.GetFileName(_file.FileName);
                        ViewData["error"] = path;
                        return Json(new
                        {
                            Success = path
                        });
                    }
                    else
                    {
                        return Json(new
                        {
                            fileError = "Only Support PDF"
                        });
                    }
                }
                else
                {
                    return Json(new
                    {
                        error = "Please Select the file"
                    });
                }
            }
        }
        return Json(new
        {
            error = "Please Select the file"
        });
    }

【问题讨论】:

  • 请检查您的pdf文件大小,可能是MVC应用程序4MB中的默认文件上传大小较大,您需要增加请求长度maxRequestLength
  • 好的兄弟非常感谢你,它只允许 4 mb,当我上传 3 mb pdf 文件时,它工作得非常好,谢谢
  • 欢迎 Bhai,如果您想上传 >4 mb 的文件,您需要使用 maxRequestLength 增加 web.config 文件的大小。

标签: c# jquery html ajax asp.net-mvc-4


【解决方案1】:

如果要上传大文件,则需要更改配置。

更多详情请点击这里Click me

您需要添加/更新“executionTimeout”的值, “maxRequestLength”和“maxAllowedContentLength”属性(如果不是) 已经在“Web.config”文件中添加了,如下图。

<system.web>  
  <authentication mode="None" />  
  <compilation debug="true" targetFramework="4.5.2" />   // here your project version
  <!-- executionTimeout = 30hrs (the value is in seconds) and maxRequestLength = 1GB (the value is in Bytes) -->  
  <httpRuntime targetFramework="4.5.2" executionTimeout="108000" maxRequestLength="1073741824" />  
</system.web>     
<system.webServer>  
  <!-- maxAllowedContentLength = 1GB (the value is in Bytes) -->      
  <security>  
    <requestFiltering>  
      <requestLimits maxAllowedContentLength="1073741824" />  
    </requestFiltering>  
  </security>
</system.webServer>  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-23
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 2012-08-28
    • 2017-02-09
    相关资源
    最近更新 更多