C#实现文件上传以及多文件上传功能

2019-12-30 17:28:41于丽

注:这里我们用到了jquery

4、前端代码


<script type="text/javascript">
  var uploadifyOnSelectError;
  var uploadifyOnUploadError;
  var uploadifyOnSelect;
  var uploadifyOnUploadSuccess;
  uploadifyOnSelectError = function (file, errorCode, errorMsg) {
    var msgText = "上传失败n";
    switch (errorCode) {
      case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
        //this.queueData.errorMsg = "每次最多上传 " + this.settings.queueSizeLimit + "个文件";
        msgText += "每次最多上传 " + this.settings.queueSizeLimit + "个文件";
        break;
      case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
        msgText += "文件大小超过限制( " + this.settings.fileSizeLimit + " )";
        break;
      case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
        msgText += "文件大小为0";
        break;
      case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
        msgText += "文件格式不正确,仅限 " + this.settings.fileTypeExts;
        break;
      default:
        msgText += "错误代码:" + errorCode + "n" + errorMsg;
    }
    layer.msg(msgText);
  };
  uploadifyOnUploadError = function (file, errorCode, errorMsg, errorString) {
    // 手工取消不弹出提示
    if (errorCode == SWFUpload.UPLOAD_ERROR.FILE_CANCELLED
      || errorCode == SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED) {
      return;
    }
    var msgText = "上传失败n";
    switch (errorCode) {
      case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
        msgText += "HTTP 错误n" + errorMsg;
        break;
      case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
        msgText += "上传文件丢失,请重新上传";
        break;
      case SWFUpload.UPLOAD_ERROR.IO_ERROR:
        msgText += "IO错误";
        break;
      case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
        msgText += "安全性错误n" + errorMsg;
        break;
      case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
        msgText += "每次最多上传 " + this.settings.uploadLimit + "个";
        break;
      case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
        msgText += errorMsg;
        break;
      case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:
        msgText += "找不到指定文件,请重新操作";
        break;
      case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
        msgText += "参数错误";
        break;
      default:
        msgText += "文件:" + file.name + "n错误码:" + errorCode + "n"
          + errorMsg + "n" + errorString;
    }
    layer.msg(msgText);
  };

  uploadifyOnSelect = function () {
  };
  uploadifyOnUploadSuccess = function (file, data, response) {
    layer.msg(file.name + "nn" + response + "nn" + data);
  };
  $(function () {

    $("#uploadify").uploadify({
      uploader: '/PageBase/UploadifyFun', //处理上传的方法
      swf: '/Scripts/lib/uploadify/uploadify.swf',
      width: 80, // 按钮宽度
      height: 60, //按钮高度
      buttonText: "上传文件",
      buttonCursor: 'hand',
      fileSizeLimit:20480,
      fileobjName: 'Filedata',
      fileTypeExts: '*.xlsx;*.docx', //扩展名
      fileTypeDesc: "请选择xslx,docx文件", //文件说明
      auto: false, //是否自动上传
      multi: true, //是否一次可以选中多个文件
      queueSizeLimit: 5, //允许同时上传文件的个数
      overrideEvents: ['onSelectError', 'onDialogClose'], // 是否要默认提示 要就不配置
      onSelect: uploadifyOnSelect,
      onSelectError: uploadifyOnSelectError,
      onUploadError: uploadifyOnUploadError,
      onUploadSuccess: uploadifyOnUploadSuccess
    });
  });
</script>
<span id="uploadify"></span>
<div>
  <a href="javascript:$('#uploadify').uploadify('upload','*');">上传</a>
  <a href="javascript:$('#uploadify').uploadify('cancel', '*');">取消</a>
</div>