2015-03-13 19 views
2
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
$(document).ready(function() { 
      $("#Button1").click(function (evt) { 
       var fileUpload = $('[id$=FileUpload1]')[0].value.split(","); 

         var data = new FormData(); 
        for (var i = 0; i < fileUpload.length; i++) { 
          data.append(fileUpload[i].name, fileUpload[i]); 
         } 

    var options = {}; 
    options.url = "Handler.ashx"; 
    options.type = "POST"; 
    options.data = data; 
    options.contentType = false; 
    options.processData = false; 
    options.success = function (result) { alert(result); }; 
    options.error = function (err) { alert(err.statusText); }; 

    $.ajax(options); 

    evt.preventDefault(); 
    }); 
}); 

這是我的jQuery和下面是我處理程序文件的代碼......我已經正確寫入了我的jquery代碼和處理程序文件代碼。但值不傳遞到處理程序文件從jQuery的文件

,直到結束我得到的價值,同時調試,但在在同時使上傳多張圖片的座右銘,我無法在處理任何價值

處理程序代碼

public void ProcessRequest (HttpContext context) { 

     string filePath = "FileSave//"; 

     foreach (string file in context.Request.Files) 
     { 
      HttpPostedFile filed = context.Request.Files[file]; 
      filed.SaveAs(context.Server.MapPath(filePath + filed.FileName)); 
      context.Response.Write("File uploaded"); 
     } 
    } 
+0

是否包含在你的''document.ready' '如果在所有它被放置在HTML – 2015-03-13 09:26:23

+0

是的,我這樣做..... – 2015-03-13 09:53:11

回答

1

你可以,如果你願意L嘗試這種方式ike去。

$(document).ready(function() { 
    $("#Button1").click(function (evt) { 
      evt.preventDefault(); 
      var formdata = new FormData(); 
      var fileInput = $('#sliderFile'); //#sliderFile is the id of your file upload control 
      if ($(fileInput).get(0).files.length == 0) 
      { //show error 
       return false; 
      } 
      else 
      { 
       $.each($(fileInput).get(0).files, function (index,value) { 
        formdata.append(value.name, value); 
       }); 
       $.ajax({ 
        url: 'Handler.ashx', 
        type: "POST", 
        dataType: 'json', 
        data: data, 
        processData: false, 
        contentType:false, 
        success: function (data) { 
          if (data.result) { 
           //return true or any thing you want to do here 
          } 
          else { 
           //return false and display error 
          } 
        }, 
        error: function (data) { 
          //return false and display error 
        } 
       }); 
      } 
    });//button click close 
});//document.ready close 

試試吧,讓我知道

編輯:請記住,但是,HTML5 FORMDATA是不是在舊的瀏覽器可用,您的代碼將靜默失敗。如果您需要支持舊的瀏覽器,你可能需要通過測試瀏覽器的功能,並回落到一個標準的表單POST進行漸進增強,如果瀏覽器不支持FormData

if(window.FormData === undefined) { 
     // The browser doesn't support uploading files with AJAX 
     // falling back to standard form upload 
} else { 
     // The browser supports uploading files with AJAX => 
     // we prevent the default form POST and use AJAX instead 
     e.preventDefault(); 

     ... 
} 

有關它的更多信息你可以看到我接受我的一個問題的答案。這很清楚,問題在哪裏。 Here is the link

編輯:只爲這些誰加入這些LINK1LINK2誰來尋找答案。

+0

不,不。 ....仍然是不可行的....通過調試我才知道我的代碼運行不錯,但只有問題是在處理程序沒有文件被請求後 – 2015-03-13 12:11:58

+0

肥胖你是否提到我在末尾提到的鏈接? – 2015-03-13 13:00:06

+0

nop nop .....我需要問一個東西......在你的鏈接代碼中你的代碼的控制器部分.....我應該在哪裏寫這個.....在哪個文件中.. ..? – 2015-03-16 05:45:49

1

使用HttpContextBase[],而不是僅僅HttpContext

相關問題