zoukankan      html  css  js  c++  java
  • 关于使用客户端控件和jquery上传文件

    一.导入Jquery插件ajaxfileupload.js
    使用方法:
    $.ajaxFileUpload({
    //type: "post",
    fileElementId: "importFile",
    url: "../ashx/BaseInfo/StationManage.ashx",
    data: { parapath: "i", action: "import" },
    dataType: "json",
    secureuri: false,
    success: function (json) {
    },
    error: function () {
    Common.ShowAlert("提示", "访问服务器失败或数据返回格式错误", function () { });
    }
    });

    注意:需要写在全局JS中,不能写在其他方法中,否则无效;

    另,input需要有name,和ID一样(不一样的情况没测试过),否则无效

    二.使用input:

    <p><span class="label">选择文件:</span><input type="file" id="importFile" name="importFile" /></p>

    三.后台获取:

    HttpFileCollection postedFile = context.Request.Files;
    HttpFileCollection postedFile2 = HttpContext.Current.Request.Files;
    string savePath = context.Server.MapPath(("/UploadFile\") + postedFile[0].FileName);//Server.MapPath 获得虚拟服务器相对路径
    
    postedFile[0].SaveAs(savePath);
    结束
     
    补充可能出现的问题
    上传成功后一直进error,进不了success
    网上说是因为Server端的Response上加上了contentType
    于是多了<pre>的标签,于是修改了ajaxUpLoad里面uploadHttpData
    的源码 ,修改如下:
    uploadHttpData: function( r, type ) {
    var data = !type;
    data = type == "xml" || data ? r.responseXML : r.responseText;
    // If the type is "script", eval it in global context
    if ( type == "script" )
    jQuery.globalEval( data );
    // Get the JavaScript object, if JSON is used.
    if (type == "json")
    ////////////以下为新增代码/////////////// 
    data = r.responseText;
    var start = data.indexOf(">");
    if (start != -1) {
    var end = data.indexOf("<", start + 1);
    if (end != -1) {
    data = data.substring(start + 1, end);
    }
    }
    ///////////以上为新增代码/////////////// 
    eval( "data = " + data );
    //eval("data = " " + data + " " ");
    // evaluate scripts within html
    if ( type == "html" )
    jQuery("<div>").html(data).evalScripts();
    
    return data;
    }

    另还查到另一种是把eval( "data = " + data );改成eval("data = " " + data + " " "); 不过对我无效

  • 相关阅读:
    清除图片周围的空白区域
    试题识别与生成
    需要继续研究
    工作中的必要举措
    教学云平台要求的硬件配置
    处理程序安装部署标准流程
    Node.js 回调函数
    git 学习
    在 Selenium 中让 PhantomJS 执行它的 API
    RF常用库简介(robotframework)
  • 原文地址:https://www.cnblogs.com/shadowtale/p/3624624.html
Copyright © 2011-2022 走看看