//个人理解:前台一个form加input[type='file'],在加一个submit的按钮
主要设置form的action,method,enctype='multipart/form-data'
* file要指定 name要不后台Request不到额,
后台:Request.files[]
file.FileName
Request.MapPath("")
file.SaveAs("")
/* 一系列业务操作 */
贴两行代码
前台
<form id="formUpload" action="/Hunter/Upload" method="post" enctype="multipart/form-data"> <table border="0" cellspacing="0" cellpadding="0" class="tableTdP5 tableMarCen"> <tr> <td class="gray-9">上传简历</td> <td> <div class="fileBox"> <input type="text" class="inp-bor"> <span class="btn">浏览</span> <input type="file" name="file" id="file" value="" class="file" /> </div> </td> <td> <input type="submit" class="baseBtn btn-lar" value="上传" /> </td> </tr> </table> </form>
后台
public void Upload() { HttpFileCollectionBase files = Request.Files; for (int i = 0; i < files.Count; i++) { HttpPostedFileBase file = files[i]; if (file.ContentLength == 0) { Response.Redirect("/url/Error"); } //获取后缀名 string houzhui = file.FileName.Substring(file.FileName.LastIndexOf(".")); string newFileName = Guid.NewGuid() + Path.GetFileName(file.FileName); var saveFilePath = Path.Combine(Request.MapPath("~/Upload"), newFileName); try { file.SaveAs(saveFilePath); SaveP_Resume(newFileName); } catch { Response.Redirect("url"); } } }