Javascript 将Fineuploader与ASP.NET webforms一起使用。对严格模式调用者函数的访问被审查
我目前在ASP.NET webforms中使用fineuploader,在FireFox中遇到了严格模式的问题。ASP.NET webforms有一个javascript文件microsoftajaxwebforms.js,其中包含以下代码,用于回发到服务器并调用传递的事件,例如下面的保存: 该函数在我正在使用的代码库中大量使用。我不能更改此代码,因为我担心产品的其他部分会出现意外的副作用。问题在于arguments.callee.caller。这就是将错误访问抛出到严格模式调用方函数的原因。我相信解决方案是从fineuploader.js中删除use strict,但我担心这会对其他浏览器中的fineuploader产生什么影响。我不熟悉javascript中的严格模式,所以也许有人可以解释一下从fineuploader.js中删除严格模式可能带来的副作用。作为参考,这里是fineuploader函数,它最终调用上述代码并导致错误Javascript 将Fineuploader与ASP.NET webforms一起使用。对严格模式调用者函数的访问被审查,javascript,c#,asp.net,webforms,fine-uploader,Javascript,C#,Asp.net,Webforms,Fine Uploader,我目前在ASP.NET webforms中使用fineuploader,在FireFox中遇到了严格模式的问题。ASP.NET webforms有一个javascript文件microsoftajaxwebforms.js,其中包含以下代码,用于回发到服务器并调用传递的事件,例如下面的保存: 该函数在我正在使用的代码库中大量使用。我不能更改此代码,因为我担心产品的其他部分会出现意外的副作用。问题在于arguments.callee.caller。这就是将错误访问抛出到严格模式调用方函数的原因。我
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中引用的代码以外的任何内容。非常感谢您的帮助。根据jQuery票证,解决方法是在客户端手动调用事件,而不是直接调用doPostBack
$('#Save').trigger('click');
但是,如果试图从客户端事件中触发回发,则触发器选项将不起作用。相反,您可以使用丑陋但可靠的setTimeout退出严格模式
两年前,jQuery最终取消了use strict,因此如果可能的话升级jQuery也可以解决这个问题。根据jQuery票证的解决方法是在客户端手动调用事件,而不是直接调用doPostBack
$('#Save').trigger('click');
但是,如果试图从客户端事件中触发回发,则触发器选项将不起作用。相反,您可以使用丑陋但可靠的setTimeout退出严格模式
两年前,jQuery最终取消了use strict,因此如果可能的话,升级jQuery也可以解决这个问题。作为传统信息,如果上传文件时需要写入数据库,实际上有一个ASP.NET WebForms VB和ASP.NET MVC示例: 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
作为传统信息,如果您在上载文件时需要执行诸如写入数据库之类的操作,实际上有一个ASP.NET WebForms VB和ASP.NET MVC示例: 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
从Fine Uploader中删除严格模式字符串应该没有问题,但我建议您不要这样做。事实上,在不准备自己拥有任何第三方代码的情况下修改任何第三方代码通常是要避免的。我已将你的问题标记为另一个类似问题的重复。请参阅Kangax关于更好解决方案的答案。可能是@raynocholus的副本,这非常有用。但我不知道如何将其应用于bind事件。如果您能多告诉我一点方向,我们将不胜感激。doPostBack在哪里声明?此外,你的问题代码中有两种不同的拼写方式。拼写是问题中的拼写错误,而不是代码__doPostBack'在MicrosoftAjaxWebForms.js中声明,它是ASP.NET Ajax的一部分。如果我理解正确的话,这是一个用WebForms打包的库。从Fine Uploader中删除strict模式字符串应该不会有问题,但我建议您不要这样做。事实上,在不准备自己拥有任何第三方代码的情况下修改任何第三方代码通常是要避免的。我已将你的问题标记为另一个类似问题的重复。请参阅Kangax关于更好解决方案的答案。可能是@raynocholus的副本,这非常有用。但我不知道如何将其应用于bind事件。如果您能多告诉我一点方向,我们将不胜感激。doPostBack在哪里声明?此外,你的问题代码中有两种不同的拼写方式。拼写是问题中的拼写错误,而不是代码__doPostBack'在MicrosoftAjaxWebForms.js中声明,它是ASP.NET Ajax的一部分。如果我理解正确的话,这是一个用WebForms打包的库。这实际上是我必须要做的,这实际上是我必须要做的