zoukankan      html  css  js  c++  java
  • AJAX + WebService 实现文件上传

    1. 界面HTML

    <p >上传文件: <input id="zfiles" type="file" name="file"/></ p>
    <br />
    <input type="button" value="上传" onclick="test()" />

    2. JavaScript代码

    function test() {
        var ts = document.getElementById("zfiles").files[0];
        var formData = new FormData();
        formData.append("file", ts);
        
         $.ajax({  
              url: '/cwbase/service/mdm/ExcelIO.asmx/UploadExcel' ,  
              type: 'POST',  
              data: formData,  
              //async: false,  
              //cache: false,  
              contentType: false,  
              processData: false,
              success: function () {  
                  alert('success');  
              },  
              error: function () {  
                  alert('error');  
              }  
         });
    }

    3. 后台代码

    [WebMethod]
    public string UploadExcel()
    {
           string result = "1";
           string filePath = "E:\";
           var file = HttpContext.Current.Request.Files;
            try
            {
                for (int i = 0; i < file.Count; i++)
                {
                    var f = file[i];
    
                     filePath = Path.Combine(filePath, f.FileName);
    
                      f.SaveAs(filePath);
                }
                    result = "0";
                }
                catch(Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                return result;
    }    
  • 相关阅读:
    eas之动态刷新Table
    eas之导入导出
    eas之事件
    eas之获得任何一个KDTable的选中行
    eas之创建一个UI界面并对其操作
    eas之style接口
    eas之指定虚模式
    eas之数据融合
    eas之kdtable格式化
    eas之视图冻结与解冻
  • 原文地址:https://www.cnblogs.com/zhchsh/p/5120693.html
Copyright © 2011-2022 走看看