会话ID未在Asp.net中返回

时间:2011-10-12 09:39:36

标签: jquery asp.net plupload

我正在尝试使用plupload JqueryUI小部件上传器并尝试使用会话ID上传图像并且它在相应的会话ID下保存,但是当On CompleteAll(即当我绑定它们)时,我试图使用相同的会话显示他们的文件名ID没有发生。

有谁能说我在做什么错误?

这是我的代码:

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

这是Handler文件:

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 个答案:

答案 0 :(得分:2)

我必须承认,我并不是那种带羽毛的家伙。但是使用Uploadify时,会话ID未被传回的原因是因为flash没有传回会话cookie。

SORRY EDIT(新链接,旧链接有安全漏洞)

以下内容可以帮助您:

Uploadify ashx file Context.Session gets null

答案 1 :(得分: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参数访问响应。