如何让用户知道他们正在提交的简历太大 [英] How to let user know the resume they are submitting is too large

查看:36
本文介绍了如何让用户知道他们正在提交的简历太大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要上传的cshtml文件,javacsript会放在该文件的什么位置?

my cshtml file for upload, where would the javacsript go in this 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>
}
}

这是我的工作代码,该代码显示如何接受简历并使用asp.net mvc保存它如果用户提交了无法处理的文件,我如何发布错误消息,指出文件大小太大.现在,如果用户提交的文件太大,则会将他们带到错误页面.我只想在屏幕上弹出一个窗口,或者告诉他们提交一个较小的文件.谢谢!

Here is my working code that shows how to accept a resume and save it using asp.net mvc How do I post an error message stating that the file size is too large if the user submits a file that cannot be handled. Right now, if a user submits a file too large, it takes them to an errors page. I just want a popup or something on the screen that tells them to submit a file that is smaller. Thanks!

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

推荐答案

您可以使用简单的javascript来做到这一点:

You can do this with simple javascript :

 <script type="text/javascript">
  $('#image-file').on('change', function() {
   var filesize = this.files[0].size/1024/1024).toFixed(2) + " MB");
   //Now can use use filesize variable and check it's value and show pop.
  });
</script>

或者您可以使用:

function GetFileSize(fileid) {
try {
    var fileSize = 0;
    //for IE
    if ($.browser.msie) {
        //before making an object of ActiveXObject, 
        //please make sure ActiveX is enabled in your IE browser
        var objFSO = new ActiveXObject("Scripting.FileSystemObject"); var filePath = $("#" + fileid)[0].value;
        var objFile = objFSO.getFile(filePath);
        var fileSize = objFile.size; //size in kb
        fileSize = fileSize / 1048576; //size in mb 
    }
        //for FF, Safari, Opeara and Others
    else {
        fileSize = $("#" + fileid)[0].files[0].size //It will calculate file size in kb

         // Put Your Alert or Validation Message Here


    }

}
catch (e) {
    alert("Error is :" + e);
}
}

或者您可以在表单提交方式中检查文件大小:

Or you can check file size on form submit as:

 <script type="text/javascript">
  $(document).ready(function(){
  $('#upload').on('click', function(e) {
   e.PreventDefault();
   var filesize = $("#File").files[0].size/1024/1024).toFixed(2));  //filesie in MB
   //Now can use use filesize variable and check it's value and show pop.
    if(parseInt(filesize)>3){ alert("File too large");return false; }
    else{ $("form").submit(); }
  });
 });
</script>

这篇关于如何让用户知道他们正在提交的简历太大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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