zoukankan      html  css  js  c++  java
  • ajax上传文件以及使用中常见问题处理

    <script src="/scripts/ajaxfileupload.js"></script>
    <script src="/scripts/jquery2.1.1.min.js"></script>
    
    <script type="text/javascript">
       $(function () {
          $(":button").click(function () {
             if ($("#fileLoad").val().length > 0) {
                ajaxFileUpload();
             }
             else {
                alert("请选择文件!");
             }
          })
       })
       function ajaxFileUpload() {
          $.ajaxFileUpload
          (
                {
                   url: '/api/poi/picture-upload', //用于文件上传的服务器端请求地址
                   type: 'post',
                   secureuri: false, //一般设置为false
                   fileElementId: 'fileLoad', //文件上传空间的id属性  <input type="file" id="fileLoad" name="file" />
                   dataType: 'json', //返回值类型
                   success: function (data, status)  //服务器成功响应处理函数
                   {
                      $("#img1").attr("src", data.data);
                      if (typeof (data.error) != 'undefined') {
                         if (data.error != '') {
                           console.log(data.error);
                         } else {
                            console.log(data.msg);
                         }
                      }
                   },
                   error: function (data, status, e)//服务器响应失败处理函数
                   {
                      console.log(data);
                   }
                }
          )
          return false;
       }
    </script>
    ajaxFileUpload 上传成功后却执行 error:返回的json数据里多了一些信息导致

    ajaxfileupload.js中加这块:
    if ( type == "json" )
        var start = data.indexOf('{');
        var end = data.indexOf('}')+1;
        data = data.substring(start,end);
        eval( "data = " + data );

    ajaxFileUpload 报这错jQuery.handleError is not a function:版本问题。

    ajaxfileupload.js中加这块:
    handleError: function( s, xhr, status, e )      {
        // If a local callback was specified, fire it
        if ( s.error ) {
            s.error.call( s.context || s, xhr, status, e );
        }
    
        // Fire the global callback
        if ( s.global ) {
            (s.context ? jQuery(s.context) : jQuery.event).trigger( "ajaxError", [xhr, s, e] );
        }
    }
  • 相关阅读:
    Excel的小游戏总结
    借助“URLScan”工具隐藏header头服务器信息
    WinCe设备连接Win10系统
    WinForm 通过HttpWebRequest实现大文件上传
    Sql 动态行转列 pivot
    C# 调用LAKALA接口获取静态二维码数据
    蜗牛星际黑群晖硬盘休眠的设置
    GIT Windows服务端搭建笔记
    C#通过socket判断FTP服务器是否通畅并判断用户名密码是否正确
    C#获取MAC地址
  • 原文地址:https://www.cnblogs.com/gq365/p/5508464.html
Copyright © 2011-2022 走看看