【问题标题】:Session ID not returning in Asp.net会话 ID 未在 Asp.net 中返回
【发布时间】:2011-10-12 19:34:12
【问题描述】:

我正在尝试使用 plupload JqueryUI Widget 上传器并尝试使用会话 ID 上传图像,并且它保存在相应的会话 ID 下,但是当在 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>

这里是处理程序文件:

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>

【问题讨论】:

    标签: jquery asp.net plupload


    【解决方案1】:

    我必须承认我不是那么熟悉 plupload。但是在 Uploadify 中,没有回传 session id 的原因是 flash 没有回传 session cookie。

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

    这里有一些可以帮助你的东西:

    Uploadify ashx file Context.Session gets null

    【讨论】:

    • 是的,我确实同意它实际上在 MVC 中,但我在普通的 Asp.net 中需要它。
    • 嗨@user944919,不要被问题的MVC标题所困扰。它仍然适用于 asp.net 网络表单,我自己使用了 asp.net 网络表单的答案,它有助于上传。因此,它可能会为您指明正确的方向。
    • 不幸的是,我对 plupload 不太熟悉,所以我不会提供太多帮助。我建议研究与页面加载相反的 HttpModules,因为它可以让您了解代码的工作原理。
    • 或者如果你熟悉uploadify,我也很满意。
    • 我不记得我是如何开始使用 asp.net/uploadify 的,但这看起来是一篇让您入门的好文章。 stackoverflow.com/questions/2501037/…
    【解决方案2】:

    我不熟悉 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>");
    });
    

    代码部分,即+ "&gt;&lt;a href='uploads/" &amp; Session("ID") &amp; "/" + 不是有效的 java 脚本代码。除了不正确的&amp; 运算符之外,您无法真正在客户端访问服务器端会话。

    我相信正确的解决方案将包括在您的处理程序的响应中返回您的服务器端 ID (Session("ID"))。您可能可以通过FileUploaded 事件处理程序的res 参数访问响应。

    【讨论】:

    • 所以我将其删除并替换为以下代码: $('#showfilelist').append("
      ");但仍然没有发生。
    • @user944919,IMO,你没明白 - 用 img 替换 a 不会有帮助。问题是您无法在 js 中访问 Session(除了 &amp; 运算符在 js 中的含义不同 - 使用 + 运算符)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多