2

我正在尝试在 VB 中使用 ASP.NET设置 Valums 文件上传器(https://github.com/valums/file-uploader )。该文件当前正在上传和存储在我想要的位置并且似乎正在工作,但是似乎 Firefox 和 Chrome(我也假设 Safari)都在正在上传的实际 CSV 文件中附加了一些额外的文本。

例如,这是我在通过 Firefox 上传的文件中看到的:

-----------------------------31203180683076
Content-Disposition: form-data; name="qqfile"; filename="test.csv"
Content-Type: application/vnd.ms-excel

/* ACTUAL CONTENTS OF FILE HERE */

-----------------------------31203180683076--

同样,Chrome:

------WebKitFormBoundarytB00bSQcafSOAnmq
Content-Disposition: form-data; name="qqfile"; filename="test.csv"
Content-Type: application/vnd.ms-excel

/* ACTUAL CONTENTS OF FILE HERE */

------WebKitFormBoundarytB00bSQcafSOAnmq--

如果我在 Chrome 中查看网络面板,我确实看到上面发布的内容是在请求有效负载中发送的,但是我想删除它,或者在服务器上写文件时完全忽略它。当我添加paramsInBody: false到上传器时,我以为我已经找到了解决方案,它确实消除了一些混乱,但不是全部。

下面是客户端的相关代码:

function createUploader() {
    var uploader = new qq.FineUploader({
        element: document.getElementById('bootstrapped-fine-uploader'),
        request: {
            paramsInBody: false,
            endpoint: 'Uploader.ashx'
        },
        validation: {
            allowedExtensions: ['csv', 'txt']
        },
        text: {
            uploadButton: '<div><i class="icon-upload-alt icon-white"></i> Browse for files...</div>'
        },
        template: '<div class="span12"><div class="qq-uploader">' +
                  '<pre class="qq-upload-drop-area span12"><span>{dragZoneText}</span></pre>' +
                  '<div class="qq-upload-button btn btn-success" style="width: auto;">{uploadButtonText}</div>' +
                  '<span class="qq-drop-processing"><span class="qq-drop-processing-spinner"></span></span>' +
                  '<ul class="qq-upload-list" style="margin: 0; list-style-type: none; margin-top: 20px;"></ul>' +
                '</div></div>',
        classes: {
            success: 'alert alert-success',
            fail: 'alert alert-error'
        }
    });
}

这是相关的服务器端处理程序:

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    On Error GoTo upload_error

    Dim Request = context.Request
    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)
        End If
    End If

    ' Now 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

    ' Or write it to the filesystem:
    Dim writeStream As FileStream = New FileStream("C:\TEMP\" & System.Guid.NewGuid.ToString() & ".csv", FileMode.Create)
    Dim bw As New BinaryWriter(writeStream)
    bw.Write(fileContents)
    bw.Close()

    ' it all worked ok so send back SUCCESS is true!
    context.Response.Write("{""success"":true}")
    Exit Sub

upload_error:
    context.Response.Write("{""error"":""An Error Occured""}")
End Sub

关于为什么它会以这种方式工作的任何想法?非常感谢。

4

1 回答 1

0

我能够通过删除 VB 代码中的浏览器检查来解决这个问题。我没有检查 IE,而是让它在所有浏览器上运行。

在运行调试时,我注意到一些东西被拉出,我想使用条件内的代码拉出,所以我尝试使用 Chrome 运行它,它运行良好。

VB 的代码现在看起来像这样。我所要做的就是注释掉条件并保持内部完整:

'Removed the If conditional and it worked.
'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)
    End If
'End If
于 2013-03-19T19:16:59.317 回答