2011-10-12 43 views
1

我想使用plupload JqueryUI小部件上傳和嘗試上傳圖像使用會話ID和它保存在各自的會話ID下,但是當onAllAll(即當我綁定它們)事件我試圖使用相同的會話ID顯示其文件名不會發生。會話ID不在Asp.net中返回

任何人都可以說我在做什麼錯誤?

這裏是我的代碼:

 <script type="text/javascript"> 
       $(function() { 
      $("#uploader").plupload({ 
       runtimes: 'gears,flash,silverlight,browserplus,html5', 
       url: 'upload.aspx', 
       max_file_size: '10mb', 
       chunk_size: '1mb', 
       unique_names: true, 

       // Resize images on clientside if we can 
       resize: { width: 320, height: 240, quality: 90 }, 

       // Specify what files to browse for 
       filters: [ 
      { title: "Image files", extensions: "jpg,gif,png" }, 
      { title: "Zip files", extensions: "zip" } 
     ], 

       // Flash settings 
       flash_swf_url: 'js/plupload.flash.swf', 

       // Silverlight settings 
       silverlight_xap_url: 'js/plupload.silverlight.xap' 
      }); 


      // Client side form validation 
      $('form').submit(function (e) { 
       var uploader = $('#uploader').plupload('getUploader'); 

       // Files in queue upload them first 
       if (uploader.files.length > 0) { 
        // When all files are uploaded submit form 
        uploader.bind('StateChanged', function() { 
         if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) { 
          $('form')[0].submit(); 
         } 
        }); 

        uploader.start(); 
       } else 
        alert('You must at least upload one file.'); 

       return false; 
      }); 

      var uploader = $('#uploader').plupload('getUploader'); 
      uploader.bind('FileUploaded', function (up, file, res) { 
      $('#showfilelist').append("<div id=" + file.id + "><a href='uploads/" & Session("ID") & "/" + file.name + "'><br>" + file.name + "</div>"); 

      }); 
     }); 
</script> 

這裏是處理程序文件:這裏

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    If IsNothing(Request.Form("chunk")) = False Then 
     If Session("ID") Is Nothing Then 
      Session("ID") = Guid.NewGuid.ToString 
      IO.Directory.CreateDirectory(Server.MapPath("uploads/" & Session("ID"))) 
     End If 
     Dim chunk As Integer = Request.Form("chunk") 
     Dim chunks As Integer = Request.Form("chunks") 
     Dim filename As String = Request.Form("name") 
     If chunk = 0 Then 
      Request.Files(0).SaveAs(Server.MapPath("uploads/") & Session("ID") & "/" & Request.Files(0).FileName) 
     Else 
      Dim OutputStream As New IO.FileStream(Server.MapPath("uploads/") & Session("ID") & "/" & Request.Files(0).FileName, IO.FileMode.Append) 
      Dim InputBytes(Request.Files(0).ContentLength) As Byte 
      Request.Files(0).InputStream.Read(InputBytes, 0, Request.Files(0).ContentLength) 
      OutputStream.Write(InputBytes, 0, InputBytes.Length - 1) 
      OutputStream.Close() 
     End If 
    End If 
End Sub 

顯示文件名:

<div id="showfilelist"> 
    </div> 

回答

2

我必須承認,我不與熟悉並且plupload。但是,使用Uploadify時,會話ID未被傳回的原因是因爲Flash不會傳回會話cookie。

SORRY EDIT(新鏈接,舊有安全漏洞)

這裏是東西,這將幫助您:

Uploadify ashx file Context.Session gets null

+0

是的我確實同意它實際上是在MVC中,但我需要它在普通的Asp.net中。 – coder

+0

Hi @ user944919,不要放在MVC標題的問題上。它仍然適用於asp.net web表單,我使用asp.net webforms自己的答案,並幫助uploadify。所以它可能會指向你正確的方向。 –

+0

您可以添加代碼或指導修改它的位置。 – coder

1

我不熟悉plupload但我看到有在你的JavaScript當然是問題。檢查這個特殊的片段:

var uploader = $('#uploader').plupload('getUploader'); 
uploader.bind('FileUploaded', function (up, file, res) { 
    $('#showfilelist').append("<div id=" + file.id + "><a href='uploads/" & Session("ID") & "/" + file.name + "'><br>" + file.name + "</div>"); 
}); 

的代碼即部分+ "><a href='uploads/" & Session("ID") & "/" +是不是一個有效的Java腳本代碼。除了不正確的&運營商,您不能真正訪問客戶端的服務器端會話。

我相信正確的解決方案將涉及返回您的處理程序的響應您的服務器端ID(Session("ID"))。您可能可以通過res參數FileUploaded事件處理程序訪問響應。

+0

所以我刪除它,並用下面的代碼替換:$('#showfilelist')。append(「

」);但仍然沒有發生。 – coder

+0

@ user944919,國際海事組織,你沒有得到它 - 用'img'代替'a'不會有幫助。問題是你不能在js中訪問'Session''(除了'&'運算符在js中有不同的含義 - 使用'+'運算符)。 – VinayC