使用Fineuploader与ASP.NET web表单。 进入严格的模式主叫功能被删(Usin

2019-10-21 05:12发布

我目前使用fineuploader与ASP.NET WebForms和我遇到与火狐严格模式的问题。 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 。 我认为,解决方法是删除了use strict从fineuploader.js,但我担心的是如何可能在其他浏览器中实现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如果需要的话。 我感谢所有帮助。

Answer 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的最终删除use strict 2年前,所以升级的jQuery(如果可能)也应该解决这一问题。



Answer 2:

作为aditional的信息,实际上是一个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


文章来源: Using Fineuploader with ASP.NET webforms. access to strict mode caller function is censored