简体   繁体   English

Valums Uploader额外请求有效负载已添加到文件

[英]Valums Uploader Extra Request Payload Added To File

I'm trying to set up the Valums File Uploader ( https://github.com/valums/file-uploader ), using ASP.NET in VB. 我正在尝试在VB中使用ASP.NET设置Valums File Uploader( https://github.com/valums/file-uploader )。 The file is currently uploading and storing where I want it to and appears to be working, however it appears that both Firefox and Chrome (I assume Safari as well) append some additional text to the actual CSV file that is being uploaded. 该文件当前正在上传并存储到我想要的位置,并且似乎可以正常工作,但是Firefox和Chrome(我也假设是Safari)似乎都在要上传的实际CSV文件中附加了一些其他文本。

For example, this is what I see in a file uploaded through Firefox: 例如,这是我在通过Firefox上传的文件中看到的内容:

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

/* ACTUAL CONTENTS OF FILE HERE */

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

And similarly, Chrome: 同样,Chrome:

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

/* ACTUAL CONTENTS OF FILE HERE */

------WebKitFormBoundarytB00bSQcafSOAnmq--

If I view the network panel in Chrome I do see exactly what is posted above being sent in the Request Payload, however I would like to either remove that, or completely ignore it on the server when it goes to write the file. 如果我在Chrome中查看网络面板,则可以确实看到上面在请求有效负载中发送的内容,但是我想删除该内容,或者在服务器上写入文件时完全忽略它。 I thought I had found the solution when I added paramsInBody: false to the uploader, and it did remove some of the mess, but not all of it. 我以为我在上载程序中添加了paramsInBody: false时就找到了解决方案,它确实删除了一些混乱的情况,但并非全部。

Below is the relevant code on the client-side: 以下是客户端上的相关代码:

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'
        }
    });
}

And here is the relevant server-side handler: 这是相关的服务器端处理程序:

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

Any ideas on why it would be working this way? 关于它为什么这样工作的任何想法? Thanks a lot. 非常感谢。

I was able to fix the issue by removing the browser check in the VB code. 我可以通过删除VB代码中的浏览器检查来解决此问题。 Instead of checking for IE, I just let it run for all browsers. 我没有检查IE,而是让它在所有浏览器中运行。

While running a Debug I noticed that some of the stuff was pulled out that I wanted to be pulled out using the code inside that conditional, so I tried running it using Chrome and it ran through fine. 在运行Debug时,我注意到有一些东西被拔出了,我想使用该条件内的代码将其拔出,因此我尝试使用Chrome运行它,并且运行得很好。

The code now looks like this for VB. 现在,VB的代码如下所示。 All I had to do was comment out the conditional and leave the insides intact: 我要做的就是注释掉该条件,并完整保留内部内容:

'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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM