<script src="JS/jquery-1.8.3.js"></script> <script src="uploadify/jquery.uploadify.min.js"></script> <script src="uploadify/jquery.uploadify.js"></script> <link href="uploadify/uploadify.css" rel="stylesheet" /> <script type="text/javascript"> $(function () { $('#file_upload').uploadify({ 'swf': '/uploadify/uploadify.swf',//必须 'uploader': '/Home/Upload',//处理上传图片的后台地址 'cancelImg': 'image/ico/uploadify-cancel.png',//取消的图片 'fileObjName': 'file', // 上传参数名称,没有这个和后台参数名对应的属性后台无法接收文件 'auto': false//设为false 可以禁止自动上传,默认true // Put your options here }); }); </script> <input type="file" name="file_upload" id="file_upload" /> <a href="javascript:$('#file_upload').uploadify('upload','*')">上传</a>| <a href="javascript:$('#file_upload').uploadify('stop','*')">取消上传</a
后台:
public ActionResult Upload( HttpPostedFileBase file) { if (file == null) { return Content("没有文件!", "text/plain"); } if (!Directory.Exists(Request.MapPath("~/Upload"))) { Directory.CreateDirectory(Request.MapPath("~/Upload")); } var fileName = Path.Combine(Request.MapPath("~/Upload"), Path.GetFileName(file.FileName)); try { file.SaveAs(fileName); //数据库操作 //... //tm.AttachmentPath = fileName;//得到全部model信息 return Content("上传成功!", "text/plain"); // return RedirectToAction("Show", tm); } catch { return Content("上传异常 !", "text/plain"); } }