2

Plupload を使用して複数の画像をサーバーにアップロードする VB.net コードを作成しました。HTTPHandler (FileUpload.ashx) を使用してアップロードを行っており、各イメージ ファイル名を SQL データベースに挿入する SQL ステートメントを追加したいと考えています。SQL を Handler に追加しようとしましたが、そうすると、アップロードされた iamge ごとに 4 つのデータベース エントリが取得されます。理由が本当にわからないので、ガイダンスが必要です。お時間をいただきありがとうございます。

関連する HANDLER コード:

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

    Dim chunk As Integer = If(context.Request("chunk") IsNot Nothing, Integer.Parse(context.Request("chunk")), 0)
    Dim fileName As String = If(context.Request("name") IsNot Nothing, context.Request("name"), String.Empty)
    Dim fileUpload As HttpPostedFile = context.Request.Files(0)

    Dim uploadPath = context.Server.MapPath("Upload")
    Using fs = New FileStream(Path.Combine(uploadPath, fileName), If(chunk = 0, FileMode.Create, FileMode.Append))
        Dim buffer = New Byte(fileUpload.InputStream.Length - 1) {}
        fileUpload.InputStream.Read(buffer, 0, buffer.Length)

        fs.Write(buffer, 0, buffer.Length)
    End Using
    context.Response.ContentType = "text/plain"
    context.Response.Write("Success")

EXP: SQL 挿入

        Dim conn As SqlClient.SqlConnection = New SqlClient.SqlConnection(DBCONN)
    Dim command As SqlClient.SqlCommand = New SqlClient.SqlCommand("W2_InsertPhoto " & fileName, conn)
    Dim rs As SqlClient.SqlDataReader
    conn.Open()
    rs = command.ExecuteReader()
    rs.Close()
    rs = Nothing
    conn.Close()
    conn = Nothing
4

2 に答える 2

1

チャンクを使用している場合は、SQLアスターを起動して最後のチャンクが保存されていることを確認してください

例えば。

  chunk = If(context.Request("chunk") IsNot Nothing, Integer.Parse(context.Request("chunk")), 0)
  chunks = If(context.Request("chunks") IsNot Nothing, Integer.Parse(context.Request("chunks")) - 1, 0) 


 If (chunk = chunks) Then
      'Upload is complete, Save to DB here or whatever
 end if

チャンクは最後のチャンクから-1であるため、CHUNKSでは-1が使用されます。

ファイル名を取得するには、handler.ashxに追加する必要があるのは.です。

fileName = If(context.Request("name") IsNot Nothing, context.Request("name"), String.Empty)

Pluplaodからハンドラーに一意のfileNameを取得するには、(クライアント上の)Pluploadに一意の名前を使用するように指示する必要があります。

var uploader = new plupload.Uploader({
        runtimes: 'html5,flash,silverlight,html4',
        max_file_size: '20mb',
        url: '../handler.ashx',
        chunk_size: '100kb',
        unique_names: true,
        multipart_params: { imageType: $('#myDiv').attr("MyIMageType"), custom: 'This is static custom text' },

ハンドラーで'name'リクエストを再度呼び出すと、pluplaoderが作成したunqie名が付けられます。また、通常どおりマルチパートのデータをリクエストします。request

PictureType = If(context.Request("imageType") IsNot Nothing, [Enum].Parse(GetType(PictureType), context.Request("imageType")), Nothing)


Dim myCustom as String = If(context.Request("custom") IsNot Nothing, context.Request("custom"))

SQLに応答して、ファイル名をスペースでカプセル化する必要があります。'そうしないと、SQLはSQLCommandを純粋に文字列として扱うのではなく、別の変数またはコマンドと見なすため、特殊文字はSQLCommandを壊します。これはSQLインジェクションの一般的な問題でもあります。このようなコードのためにハッカーがコードをインジェクションできるようにします。

于 2012-04-12T16:18:04.973 に答える
1

ppumpkin、私は自分自身をうまく説明しているとは思いません。申し訳ありませんが、私はpluploadとハンドラーをすべて同時に行うのは初めてです。

各ファイルの元の名前を保持する必要があるため、一意の名前を「false」として使用しています。現在、サーバーへのアップロード時にファイル名に正しい名前を付けていますが、SQL挿入には同じ名前を挿入する必要があります。宣言した FileName (context.Request("name")) を SQL ステートメントの値として使用しようとすると、すぐにエラーが発生し、値が挿入されません。テストのためにファイル名に静的な値を使用すると、問題なく挿入されますが、アップロードする各ファイルの名前はもちろん同じです。

あなたの更新を含めて、これは私が現在ハンドラーとクライアントのスクリプトに持っているものです。

ハンドラ:

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

    Dim chunk As Integer = If(context.Request("chunk") IsNot Nothing, Integer.Parse(context.Request("chunk")), 0)
    Dim chunks As Integer = If(context.Request("chunks") IsNot Nothing, Integer.Parse(context.Request("chunks")) - 1, 0)
    Dim fileName As String = If(context.Request("name") IsNot Nothing, context.Request("name"), String.Empty)

    If (chunk = chunks) Then
        Dim conn As SqlClient.SqlConnection = New SqlClient.SqlConnection(mdata.DBCONN)
        Dim command As SqlClient.SqlCommand = New SqlClient.SqlCommand("W2_InsertPhoto 12345," & **fileName**, conn)
        Dim rs As SqlClient.SqlDataReader
        conn.Open()
        rs = command.ExecuteReader()
        rs.Close()
        rs = Nothing
        conn.Close()
        conn = Nothing
    End If

    Dim fileUpload As HttpPostedFile = context.Request.Files(0)

    Dim uploadPath = context.Server.MapPath("Upload")
    Using fs = New FileStream(Path.Combine(uploadPath, fileName), If(chunk = 0, FileMode.Create, FileMode.Append))
        Dim buffer = New Byte(fileUpload.InputStream.Length - 1) {}
        fileUpload.InputStream.Read(buffer, 0, buffer.Length)
        fs.Write(buffer, 0, buffer.Length)
    End Using
End Sub

私のクライアントスクリプト:

    <script type="text/javascript">
    // Convert divs to queue widgets when the DOM is ready
    $(function () {
        $("#uploader").pluploadQueue({
            // General settings,silverlight,browserplus,html5gears,
            runtimes: 'flash',
            url: 'FileUpload.ashx',
            max_file_size: '10mb',
            chunk_size: '1mb',
            unique_names: false,

            // Specify what files to browse for
            filters: [{ title: "Image files", extensions: "jpg,jpeg,gif,png,bmp"}],
            // Flash settings
            flash_swf_url: 'assets/resources/plupload.flash.swf',


            // Silverlight settings
            silverlight_xap_url: 'assets/resources/plupload.silverlight.xap',

            init: {
                FileUploaded: function (up, file, info) {
                }
            }
        });

        // Client side form validation
        $('form').submit(function (e) {
            var uploader = $('#uploader').pluploadQueue();

            // Validate number of uploaded files
            if (uploader.total.uploaded == 0) {
                // Files in queue upload them first
                if (uploader.files.length > 0) {
                    // When all files are uploaded submit form
                    uploader.bind('UploadProgress', function () {
                        if (uploader.total.uploaded == uploader.files.length)
                            $('form').submit();
                    });
                    uploader.start();
                } else
                    alert('You must at least upload one file.');

                e.preventDefault();
            }
        });
        //tweak to reset the interface for new file upload
        $('#btnReset').click(function () {
            var uploader = $('#uploader').pluploadQueue();

            //clear files object
            uploader.files.length = 0;

            $('div.plupload_buttons').css('display', 'block');
            $('span.plupload_upload_status').html(''); 
            $('span.plupload_upload_status').css('display', 'none');
            $('a.plupload_start').addClass('plupload_disabled');
            //resetting the flash container css property
            $('.flash').css({
                position: 'absolute', top: '292px',
                background: 'none repeat scroll 0% 0% transparent',
                width: '77px',
                height: '22px',
                left: '16px'
            });
            //clear the upload list
            $('#uploader_filelist li').each(function (idx, val) {
                $(val).remove();
            });
        });
    });
</script>
于 2012-04-13T14:41:52.603 に答える