码迷,mamicode.com
首页 > Web开发 > 详细

MVC上传文件

时间:2016-01-29 19:53:40      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

1、首先需要定义页面标签

<form id="fileUploadForm" method="post" enctype="multipart/form-data">

</form>

2、定义JQuery方法

function file_onChange(srcElement) {
 $("#fileUploadForm").ajaxSubmit({
  type: post,
  url: "/require/UpLoadFiless",
  data: { Upload_req00_no: $("#Upload_req00_no").val() },
  success: function (result) {
    //上传完成后的操作
   if (result != "") {
    result = eval("(" + result + ")");
    var filenamestr = result.fileName + "." + result.fileExtension;
    var obj = $("#div_upload_info", parent.document)
    $(obj).append("<span class=‘tagstyle‘><input  type=‘hidden‘ name=‘fileExtension‘ value=" + result.fileExtension + "><input  type=‘hidden‘ name=‘fileName‘ value=" + result.fileName + "><input  type=‘hidden‘ name=‘fileSize‘ value=" + result.fileSize + "><a href=‘#‘ onclick=‘DownloadFiles(this,\"" + filenamestr + "\",\"" + $("#Upload_req00_no").val() + "\")‘>" + result.fileName + "." + result.fileExtension + "</a><a class=‘ico-href‘ href=‘#‘><i class=‘icon-remove‘ onclick=removeTag(this)></i></a></span>");
   }
  },
  error: function (XmlHttpRequest, textStatus, errorThrown) {
   alert("error");
  }
 });

3、控制器中的操作

HttpPostedFileBase uploadFile = Request.Files["file"];
if (Request.Files.Count > 0)
    {
     if (uploadFile != null)
     {
      byte[] lByteArray;
      MemoryStream lMemoryStream = new MemoryStream();
   //将文件流拷贝到内存流中 uploadFile.InputStream.CopyTo(lMemoryStream); lByteArray
= lMemoryStream.ToArray(); path = string.Empty; path = Upload_req00_no;
   //获取文件后缀 fileExtension
= uploadFile.FileName.Substring(uploadFile.FileName.LastIndexOf(.) + 1);
   //获取文件名称 fileName
= uploadFile.FileName.Substring(0, uploadFile.FileName.LastIndexOf(.)); UpLoadFileModel fModel = new UpLoadFileModel();
    //文件大小 fileSize
= uploadFile.ContentLength;
    //自定义的API服务来保存文件
base.ExecAPIService(EnumAppSettingAPIKey.APIMELMRPAttachFileUploadEventURL, fModel.GetAPIUploadEntity(path, fileName, fileExtension), lByteArray); }

 

MVC上传文件

标签:

原文地址:http://www.cnblogs.com/zhangweidong/p/5169230.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!