【问题标题】:Using Fineuploader with ASP.NET webforms. access to strict mode caller function is censored将 Fineuploader 与 ASP.NET 网络表单一起使用。对严格模式调用函数的访问被审查
【发布时间】:2014-11-03 16:43:36
【问题描述】:

我目前正在将 Fineuploader 与 ASP.NET 网络表单一起使用,并且在 FireFox 中遇到了严格模式的问题。 ASP.NET webforms 有一个 javascript 文件 (microsoftajaxwebforms.js),其中包含以下代码(用于回发到服务器并调用传递的事件,例如下面的 Save。):

_doPostBack: function(a, k) {
    var f = window.event;
    if (!f) {
        var d = arguments.callee ? arguments.callee.caller : null;
        if (d) {
            var j = 30;
            while (d.arguments.callee.caller && --j) d = d.arguments.callee.caller;
            f = j && d.arguments.length ? d.arguments[0] : null
        }
    }
    ...

该函数在我正在使用的代码库中被大量使用。我无法更改此代码,因为担心产品的其余部分会出现意外的副作用。问题在于arguments.callee.caller。这就是引发错误access to strict mode caller function is censored 的原因。我相信解决方案是从fineuploader.js 中删除use strict,但我担心这会如何影响其他浏览器中的fineuploader。我不熟悉javascript中的严格模式,所以也许有人可以阐明从fineuploader.js中删除严格模式的可能副作用。供参考,这里是最终调用上述代码并导致错误的fineuploader函数。

var fineUploader = $('#jquery-wrapped-fine-uploader').fineUploader({
    ...
    multiple: false,
    text: {
        uploadButton: 'Click or drag a file to upload.'
    },
    autoUpload: false,
    debug: false,
    template: 'fineuploader-template',
    ...
    }
}).bind('complete', function (event, id, name, response) {
    if (response['success']) {
        cp_hide();
        fineUploader.fineUploader('reset');
        __doPostBack("Save", "");
    }
})...

如果需要,我可以修改 microsoftajaxwebforms.js 引用的代码以外的任何内容。感谢您的帮助。

【问题讨论】:

  • 从 Fine Uploader 中删除严格模式字符串应该没有问题,但我建议您不要走那条路。事实上,在不准备自己拥有的情况下修改任何第三方代码通常是可以避免的。我已将您的问题标记为另一个类似问题的副本。有关更好的解决方案,请参阅 Kangax 的答案。
  • @RayNicholus 这真的很有帮助。我不确定如何将其应用于bind 事件。多一点方向将不胜感激。
  • __doPostBack 在哪里声明?此外,您的问题代码中有两种不同的拼写方式。
  • 拼写是问题而不是代码中的拼写错误。 __doPostBack' is declared in MicrosoftAjaxWebForms.js`,它是 ASP.NET Ajax 的一部分。如果我理解正确的话,它是一个与 WebForms 一起打包的库。

标签: javascript c# asp.net webforms fine-uploader


【解决方案1】:

根据jQuery票证(http://bugs.jquery.com/ticket/13335)的解决方法是在客户端手动调用事件而不是直接调用__doPostBack

$('#Save').trigger('click');

但是,如果您尝试从客户端事件中触发回发,trigger 选项将不起作用。相反,您可以使用丑陋但可靠的setTimeout 来退出strict 模式。

$('#Save').on('click', function(e) {
  var result = doSomeStuff();
  if(result.success) {
    window.setTimeout(function() { __doPostBack('Save', '') }, 5);
  }
  // ...
});

jQuery 最终在 2 年前删除了 use strict,因此升级 jQuery(如果可能)也应该可以解决问题。

【讨论】:

  • 这实际上是我最终不得不做的事情
【解决方案2】:

作为附加信息,实际上有一个 ASP.NET WebForms VB 和 ASP.NET MVC C# 示例,如果您需要在上传文件时执行诸如写入数据库之类的操作:

VB 示例:

Imports System.Data.SqlClient
Imports System.Net
Imports System.IO
Namespace Uploader
    Public Class UploadController
        Inherits System.Web.Mvc.Controller

        <HttpPost()> _
        Function Upload(ByVal uploadFile As String) As String
            On Error GoTo upload_error
            Dim strm As Stream = Request.InputStream
            Dim br As BinaryReader = New BinaryReader(strm)
            Dim fileContents() As Byte = {}
            Const ChunkSize As Integer = 1024 * 1024

 ' We need to hand IE a little bit differently...
            If Request.Browser.Browser = "IE" Then
                Dim myfiles As System.Web.HttpFileCollection = System.Web.HttpContext.Current.Request.Files
                Dim postedFile As System.Web.HttpPostedFile = myfiles(0)
                If Not postedFile.FileName.Equals("") Then
                    Dim fn As String = System.IO.Path.GetFileName(postedFile.FileName)
                    br = New BinaryReader(postedFile.InputStream)
                    uploadFile = fn
                End If
            End If

' Nor have the binary reader on the IE file input Stream. Back to normal...
            Do While br.BaseStream.Position < br.BaseStream.Length - 1
                Dim b(ChunkSize - 1) As Byte
                Dim ReadLen As Integer = br.Read(b, 0, ChunkSize)
                Dim dummy() As Byte = fileContents.Concat(b).ToArray()
                fileContents = dummy
                dummy = Nothing
            Loop


            ' You now have all the bytes from the uploaded file in 'FileContents'

            ' You could write it to a database:

            'Dim con As SqlConnection
            'Dim connectionString As String = ""
            'Dim cmd As SqlCommand

            'connectionString = "Data Source=DEV\SQLEXPRESS;Initial Catalog=myDatabase;Trusted_Connection=True;"
            'con = New SqlConnection(connectionString)

            'cmd = New SqlCommand("INSERT INTO blobs VALUES(@filename,@filecontents)", con)
            'cmd.Parameters.Add("@filename", SqlDbType.VarChar).Value = uploadFile
            'cmd.Parameters.Add("@filecontents", SqlDbType.VarBinary).Value = fileContents
            'con.Open()
            'cmd.ExecuteNonQuery()
            'con.Close()


            ' Or write it to the filesystem:
            Dim writeStream As FileStream = New FileStream("C:\TEMP\" & uploadFile, FileMode.Create)
            Dim bw As New BinaryWriter(writeStream)
            bw.Write(fileContents)
            bw.Close()

            ' it all worked ok so send back SUCCESS is true!
            Return "{""success"":true}"
            Exit Function

upload_error:
            Return "{""error"":""An Error Occured""}"
        End Function
    End Class
End Namespace

【讨论】:

    猜你喜欢
    • 2013-01-10
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    相关资源
    最近更新 更多