如何在ASP.NET MVC5中通知用户他们提交的文件太大?

时间:2014-08-03 20:19:54

标签: c# asp.net-mvc resume

我基本上想告诉用户他们提交的文件是否太大。现在,它重定向到错误页面。如何提交大文件,如何获取基本弹出窗口或只是写入屏幕,说文件太大。 ASP.NET MVC5具有设置的最大长度,因此我无法定义大小限制。请帮助,已经尝试了一段时间没有结果。谢谢

VIEW, CSHTML FILE
@{
ViewBag.Title = "Upload";
}

<div id="progressbar">
<div id="progressbar" class="all-rounded" style="width: 20%"><span></span></div>
</div>

<div class="jumbotron">

<h2>Job Application Management System</h2>
<p class="lead2"> Welcome @((string)(ViewData["FullName"])), Please upload your resume here. Thank you!</p>
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype =     "multipart/form-data" }))
{
    <table>
        <tr>
            <td> <label> &nbsp; </label> <input type="file" class="btn btn-default"   name="File" id="File" /></td>
        </tr>
        <tr>
            <td><input type="submit" class="btn btn-default" id="upload" value="Upload" /></td>
        </tr>
    </table>
    if (TempData["notice"] != null)
    {
        <p>@TempData["notice"]</p>
        <p><button class="btn btn-default" onclick="location.href='@Url.Action("Create", "Accomplishments")';return false;">Continue To Accomplishments</button></p>
    }
}

public class HomeController : ApplicationController
{
    /// <summary>
    /// GET: /Home/Register
    /// </summary>
    /// <returns>View of index</returns>
    public ActionResult Index()
    {
        return View(); 
    }

    /// <summary>
    /// GET: /Home/Upload
    /// </summary>
    /// <returns>View of upload</returns>
    public ActionResult Upload(int? jobId)
    {
        return View();
    }

    /// <summary>
    /// POST: /Home/Upload
    /// </summary>
    /// <param name="resume">Resume</param>
    /// <param name="command">string</param>
    /// <returns>Resume added and continue</returns>
    [HttpPost]
    [Authorize]
    public ActionResult Upload(Resume resume, string command)
    {
        if (command == "Next")
        {
            RedirectToAction("Create", "Accomplishments");
        }
        try
        {
            if (resume.File.ContentLength > 0)
            {
                string fileName = Path.GetFileName(resume.File.FileName);
                string path = Path.Combine(Server.MapPath("~/Content/Resumes"), fileName);
                resume.File.SaveAs(path);
            }
            TempData["notice"] = "Resume Added:  "+ resume.File.FileName;
            return View(resume);


        }
        catch (Exception)
        {
            ViewBag.Message = "Upload Error";
            return View("Upload");
        }
    }

    /// <summary>
    /// Contact page (unused)
    /// </summary>
    /// <returns>View of contact</returns>
    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
}

}

1 个答案:

答案 0 :(得分:3)

这是从客户端处理的最好方法,因为无论您想要在服务器上使用什么解决方案,都需要在检查文件大小之前完全上载文件,或者至少发送一个ajax调用检测到的文件大小,最好在客户端上完全处理。

我建议的解决方案是使用以下jquery片段,我通常在需要类似检查时使用。

$(function () {
    $('#fuPicture').bind('change', function () {
          // check if the file is larger than 600kb
          if (this.files[0].size > 819200) {                                
              alert("file is larger than 600kb, please choose a smaller one.")              
          } 
        else {
                // passed.                      
             }
     });
})

前面的代码正在处理fileupload控件的(change)事件,并检查所选文件大小是否大于所需大小(600kb),然后它会警告用户所选文件大于允许的文件大小。

请注意“ fuPicture ”是您的fileupload控件的ID。并且此检查返回的大小(this.files[0].size)以字节为单位,因此您需要先将所需大小转换为字节。