2011-10-02 35 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接口。