2011-10-02 36 views
1

我使用jQuery的阿賈克斯將文件上傳到我的數據庫,產生的進度條..從ASP.NET的ashx將信息傳遞給ASPX

的問題是,我沒有上傳的文件名傳遞給我的後端的方式碼。我可以很容易地通過使用Hiddenfields和jquery通過用戶發送的文件名,但是如果該文件存在,我的ASHX Handler可能會重命名它。我試圖創造與傳遞的上下文中的ASHX文件會話[ 「變量」],但它是空..

這裏是我的javascript:

$(function() { 
    $("#<%=supplierInfo.FUclientID %>").makeAsyncUploader({ 
     upload_url: '<%=ResolveUrl("~/Controls/UploadHandler.ashx")%>', // Important! This isn't a directory, it's a HANDLER such as an ASP.NET MVC action method, or a PHP file, or a Classic ASP file, or an ASP.NET .ASHX handler. The handler should save the file to disk (or database). 
     flash_url: '../../Scripts/swfupload.swf', 
     button_image_url: '../../Scripts/blankButton.png', 
     disableDuringUpload: 'INPUT[type="submit"]', 
     upload_success_handler: function() { 
      var hiddenfield = document.getElementById('<% =hdnTest.ClientID %>'); 
      hiddenfield.value = "test"; 

      $("span[id$=_completedMessage]", container).html("Uploaded <b>{0}</b> ({1} KB)" 
         .replace("{0}", file.name) 
         .replace("{1}", Math.round(file.size/1024)) 
        ); 
     } 
    }); 
}); 

的ASHX處理程序:

<%@ WebHandler Language="C#" Class="UploadHandler" %> 

using System; 
using System.Web; 
using System.IO; 

public class UploadHandler : IHttpHandler { 

    public void ProcessRequest (HttpContext context) { 

     string strFileName = Path.GetFileName(context.Request.Files[0].FileName); 
     string strExtension = Path.GetExtension(context.Request.Files[0].FileName).ToLower(); 

     int i = 0; 
     while (File.Exists(context.Server.MapPath("~/images/Upload/SupplierImg") + "/" + strFileName)) 
     { 
      strFileName = Path.GetFileNameWithoutExtension(strFileName) + i.ToString() + strExtension; 
     } 

     string strLocation = context.Server.MapPath("~/images/Upload/SupplierImg") + "/" + strFileName; 
     context.Request.Files[0].SaveAs(strLocation); 

     context.Response.ContentType = "text/plain"; 
     context.Response.Write("OK"); 

    } 

    public bool IsReusable { 
     get { 
      return false; 
     } 
    } 

} 
+0

「我試圖創建一個會話[‘變量’]」 - 這不是在你的例子。 – bzlm

回答

0

確保您在jQuery AJAX調用中顯式聲明瞭內容類型。

$.ajax({ 
    type: "POST", 
    url: "UploadHandler.ashx", 
    data: "{ 'fileName' : 'myFileName' }", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(data) { 
      // This may be data.d depending on what version of .NET you are running 
      //  See this link for more info on .d - http://haacked.com/archive/2009/06/25/json-hijacking.aspx 

      $("span[id$=_completedMessage]", container).html("Uploaded <b>{0}</b> ({1} KB") 
        .replace("{0}", data.fileName) 
        .replace("{1}", Math.round(data.fileSize/1024)) 
      ); 
    } 
}); 

而且你可能要預先序列化服務器上​​的數據:

public void ProcessRequest (HttpContext context) { 

    string strFileName = Path.GetFileName(context.Request.Files[0].FileName); 

... 

    context.Response.ContentType = "text/plain"; 
    context.Response.Write(JsonConvert.SerializeObject(new { fileName = strFileName, fileSize = iFileSize })); 

} 
0

爲什麼不返回strFileName而不是「OK」,那麼你有文件名?

context.Response.Write(strFileName); 
0

如果你想在你的處理程序中使用Session,你需要實現IRequiresSessionState接口。