首页 > Web开发 > 详细

AJAX + WebService 实现文件上传

时间:2016-01-11 13:33:07      阅读:333      评论:0      收藏:0      [点我收藏+]

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;
}    

 

AJAX + WebService 实现文件上传

原文:http://www.cnblogs.com/zhchsh/p/5120693.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!