尝试使用ashx获取查询字符串变量

时间:2016-05-31 16:27:05

标签: asp.net vb.net ashx

我一直试图通过大量的谷歌搜索彻底解决这个问题,但影响不大,所以如果你阅读并能够提供帮助,请提前感谢。

我有一个简单的页面,允许用户选择文件并上传到亚马逊桶,第一页看起来像这样:

var checkError = false;
$(window).load(
    function () {
        $("#<%=FileImages.ClientID %>").fileUpload({
            'fileSizeLimit': '100MB',
            'uploader': 'scripts/uploader.swf',
            'cancelImg': 'images/cancel.png',
            'buttonText': 'Upload Files',
            'script': 'UploadVB.ashx?gid=56',
            'folder': 'uploads',
            'multi': true,
            'auto': true,
            'onAllComplete': function (uploads) {
                if (!checkError) {
                    location.href = "finished";
                }
            },
            'onComplete': function (event, ID, fileObj, response, data) {
                if (200 != parseInt(response)) {
                    checkError = true;
                    $('#FileImages' + ID).addClass('uploadifyError');
                    $('#FileImages' + ID + ' .percentage').text(' - ' + response);

                    return false;
                }
            }
        });
    }

);

请注意gid=56。然后它将文件发送到UploadVB.ashx,如下所示:

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    Dim awsRegion As Amazon.RegionEndpoint = Amazon.RegionEndpoint.EUWest1
    Dim client As New AmazonS3Client(System.Configuration.ConfigurationManager.AppSettings("AWSAccessKey").ToString(), System.Configuration.ConfigurationManager.AppSettings("AWSSecretKey").ToString(), awsRegion)
    Dim fileID As String = Guid.NewGuid().ToString("N")
    Dim postedFile As HttpPostedFile = context.Request.Files("Filedata")

    Dim galleryid = context.Request.QueryString("gid")

    Try
        Dim filename As String = Path.GetFileName(postedFile.FileName)
'### I WANT TO SUBMIT THE FILENAME AND ID INTO A TABLE HERE ######
    AddPortfolioPhoto(filename, Convert.ToInt16(galleryid))
    '#################################################################
        Dim S3_KEY As [String] = filename.Replace(" ", "")
        Dim request As New PutObjectRequest()
        With request
            .BucketName = System.Configuration.ConfigurationManager.AppSettings("AWSBucketName").ToString()
            .Key = S3_KEY
            .InputStream = postedFile.InputStream
        End With

        client.PutObject(request)
        context.Response.Write("200")
        context.Response.StatusCode = 200
    Catch ex As Exception
        context.Response.Write("AWS ERROR :: " + ex.Message)
    End Try
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
    Get
        Return False
    End Get
End Property

每次我在上面运行它都没有做任何事情,如果我删除这一行:

AddPortfolioPhoto(filename, Convert.ToInt16(galleryid))

它工作得很好但是至关重要的是我将它插入到数据库中并且我似乎无法获取gid值。

非常感谢任何帮助。

Public Shared Sub AddPortfolioPhoto(ByVal fn As String, gid As Integer)

    Dim Writer As New Data.SqlClient.SqlCommand
    Writer.CommandType = Data.CommandType.StoredProcedure
    Writer.CommandText = "BCMS_AddGalleryPhoto"
    Writer.Connection = New Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.AppSettings("sqlstr"))
    Writer.Parameters.AddWithValue("@imgstr", Data.SqlTypes.SqlString.Null).Value = fn
    Writer.Parameters.AddWithValue("@galleryid", Data.SqlTypes.SqlInt32.Null).Value = gid
    Writer.Parameters.AddWithValue("@so", Data.SqlTypes.SqlInt32.Null).Value = 0
    Writer.Parameters.AddWithValue("@featured", Data.SqlTypes.SqlInt32.Null).Value = 0
    Writer.Prepare()
    Writer.Connection.Open()
    Writer.ExecuteNonQuery()
    Writer.Connection.Close()

End Sub

1 个答案:

答案 0 :(得分:0)

看起来在你的帮助下我可能已经深究了这一点,我期待的价值(56)并不是我实际得到的,我实际上正在上传&#34;上传&#34;在同一个变量中,所以发送一个字符串到这个方法永远不会起作用,感谢@ matt-dot-net建议并感谢Andrew Morton提供处理提示,我将使用它而不是关闭。