问题
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.
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/26718805/using-fineuploader-with-asp-net-webforms-access-to-strict-mode-caller-function