将Fineuploader与ASP.NET webforms一起使用。访问严格模式调用函数被审查

时间:2014-11-03 16:43:36

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

我目前正在使用带有ASP.NET webforms的fineuploader,并且在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引用的代码之外的任何内容。我感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

根据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(如果可能的话)也应该解决这个问题。

答案 1 :(得分:0)

作为附加信息,实际上有一个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