1

I am currently using fineuploader with ASP.NET webforms and am encountering a problem with strict mode in FireFox. ASP.NET webforms has a javascript file (microsoftajaxwebforms.js) that contains the following code (This is used to postback to the server and call the passed event, ex. Save below.):

_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
        }
    }
    ...

That function is used liberally in the codebase I am working with. I cannot change this code for fear of unintended side-effects in the rest of the product. The problem is with the arguments.callee.caller. That is what is throwing the error access to strict mode caller function is censored. I believe the solution is to remove the use strict from the fineuploader.js, but I am worried about how that might effect fineuploader in other browsers. I am not familiar with strict mode in javascript, so maybe someone can shed some light on the possible side-effects of removing strict mode from the fineuploader.js. For reference, here is the fineuploader function that eventually calls the above code and causes the error.

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", "");
    }
})...

I can modify anything short of the code referenced from microsoftajaxwebforms.js if needed. I appreciate any help.

coryrwest
  • 631
  • 1
  • 8
  • 23
  • It shouldn't be a problem to remove the strict mode string from Fine Uploader, but I suggest you _not_ go that route. In fact, modifying any third party code without being prepared to own it yourself is usually something to avoid. I've marked your question as a duplicate of another similar question. See the answer from Kangax regarding a better solution. – Ray Nicholus Nov 03 '14 at 16:52
  • possible duplicate of [Can I disable ECMAscript strict mode for specific functions?](http://stackoverflow.com/questions/6020178/can-i-disable-ecmascript-strict-mode-for-specific-functions) – Ray Nicholus Nov 03 '14 at 16:52
  • @RayNicholus that was really helpful. I am not sure how to apply that to the `bind` event though. A little more direction would be greatly appreciated. – coryrwest Nov 03 '14 at 20:30
  • Where is `__doPostBack` declared? Also, you have it spelled two different ways in your question code. – Ray Nicholus Nov 03 '14 at 21:56
  • The spelling is a typo in the question not the code. `__doPostBack' is declared in `MicrosoftAjaxWebForms.js`, which is part of ASP.NET Ajax. If I understand correctly it is a library that is packaged with WebForms. – coryrwest Nov 03 '14 at 22:25
  • So, is that function a global? – Ray Nicholus Nov 03 '14 at 22:29

2 Answers2

4

The workaround according to the jQuery ticket (http://bugs.jquery.com/ticket/13335) is to manually call the event on the client side rather than calling __doPostBack directly.

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

However, if you are trying to trigger a postback from within the client-side event, the trigger option won't work. Instead, you can use ugly, yet trusty setTimeout to get out of strict mode.

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

jQuery eventually removed use strict 2 years ago, so upgrading the jQuery (if possible) should also solve the issue.

0

As aditional information, there's actually an ASP.NET WebForms VB and ASP.NET MVC C# examples if you need to do stuffs like write to database when uploading the files:

VB example:

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