2

VBでASP.NETを使用して、Valums File Uploader( 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 に答える