使用不是 MVC 项目的 ASP.NET需要WebHandler来无缝处理请求。有关 WebHandler 的示例和用法,请参见此处。
参考SanthoshM的答案并结合 Fine Uploader MVC VB.net Server-Side 示例,这就是我想出的。我希望这可能对某人有所帮助。
客户端
<script>
var existingHandler1 = window.onload;
window.document.body.onload = function () {
var galleryUploader = new qq.FineUploader({
element: document.getElementById("fine-uploader-gallery"),
template: 'qq-template-gallery',
request: {
endpoint: '../App_Extension/FileUpload.ashx'
},
debug: true,
thumbnails: {
placeholders: {
waitingPath: '../App_Images/waiting-generic.png',
notAvailablePath: '../App_Images/not_available-generic.png'
}
},
validation: {
allowedExtensions: ['jpeg', 'jpg', 'gif', 'png'],
sizeLimit: 3145728 // 3 MB = 3 * 1024 * 1024 bytes
},
retry: {
enableAuto: true
},
});
if (existingHandler1) { existingHandler1() }
}
</script>
服务器端
<%@ WebHandler Language="VB" Class="FileUpload" %>
Imports System
Imports System.Web
Imports System.IO
Imports System.Linq
Imports System.Drawing
Public Class FileUpload : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/plain"
'context.Response.Write("Hello World")
Dim reader As StreamReader = New StreamReader(context.Request.InputStream)
Try
Dim values As String = DateTime.Now.Millisecond.ToString + Rnd(10000).ToString + ".jpg" 'reader.ReadToEnd()
' 'BLL.WriteLog(values)
'Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(context.Request.InputStream)
' img.Save("C:\DownloadedFiles\" + DateAndTime.TimeString + ".Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
''BLL.WriteLog(values)
Dim responseText As String = Upload(values, context)
'BLL.WriteLog(responseText)
context.Response.Write(responseText)
'context.Response.Write("{""error"":""An Error Occured""}")
Catch ex As Exception
'BLL.WriteLog(ex.Message + ex.StackTrace)
End Try
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Function Upload(ByVal uploadFile As String, ByVal context As HttpContext) As String
'BLL.WriteLog("1")
On Error GoTo upload_error
Dim strm As Stream = context.Request.InputStream
Dim br As BinaryReader = New BinaryReader(strm)
Dim fileContents() As Byte = {}
Const ChunkSize As Integer = 1024 * 1024
'Dim uploadFile As String
'BLL.WriteLog("2")
' We need to hand IE a little bit differently...
' If context.Request.Browser.Browser = "IE" Then
'BLL.WriteLog("3")
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
'BLL.WriteLog("4")
' Nor have the binary reader on the IE file input Stream. Back to normal...
Do While br.BaseStream.Position < br.BaseStream.Length - 1
'BLL.WriteLog("5")
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
'BLL.WriteLog("6")
' 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:\DownloadedFiles\" & uploadFile, FileMode.Create)
'BLL.WriteLog("7")
Dim bw As New BinaryWriter(writeStream)
bw.Write(fileContents)
bw.Close()
'BLL.WriteLog("8")
' 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