• MVC上传文件


    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); }
  • 相关阅读:
    在HTML中使用css3实现雪人动画效果
    不一样的函数防抖和节流
    2020全球C++及系统软件技术大会成功落下帷幕
    详解flex布局做骰子案例
    LeetCode-环形链表|+环形链表||
    03:成绩排序
    02:奇数单增序列
    谁考了第k名
    【23.59%】【hdu 5883】The Best Path
    【30.53%】【hdu 5878】I Count Two Three
  • 原文地址:https://www.cnblogs.com/zhangweidong/p/5169230.html
Copyright © 2020-2023  润新知