开发者

Triggering a SUBMIT in an asp.net application

I need to trigger a submit when saving my image file in SQL 2005 database.

A user will upload their image file.

My application will then check to see if the image file size is within specific requirements.

If so, then save the image into a table where the field is image data type. (if size does not meet requirements, display error message and exit sub).

The issue is if I save the image file to the database, the value is saved correctly (however, I have not checked the image size).

If I check the image file size first, THEN save it, the value saved is corrupted (incorrect).

Is there a way that I can check the file size, then save the file while doing a submit in code-behind?

My code is below:

            Dim iRequiredImageHeight As Integer = 80
            Dim iRequiredImageWidth As Integer = 280

            Dim imgBytes(FileUpload1.PostedFile.InputStream.Length) As Byte
            Dim stream As System.IO.Stream = FileUpload1.PostedFile.InputStream
            stream.InitializeLifetimeService()
            stream.Read(imgBytes, 0, imgBytes.Length)

            Dim imgData As System.Drawing.Image = System.Drawing.Image.FromStream(stream)
            Dim imgWidth As Integer = imgData.PhysicalDimension.Width
            Dim imgHeight As Integer = imgData.PhysicalDimension.Height

            If imgHeight > iRequiredImageHeight Or imgHeight > iRequiredImageWidth Then
                lblLogoMessage.Text = "The image you have uploaded is " & imgHeight & "px height by " & imgWidth & "px width. <br/> Required image size is " & iRequiredImageHeight & "px height by " & iRequiredImageWidth & "px width. <br/> Please resize the image and try again."
                lblLogoMessage.ForeColor = D开发者_JS百科rawing.Color.Red
                lblLogoMessage.Visible = True
                bLogoFit = False
            Else

                Dim LogoFile As String = FileUpload1.FileName.ToString.Trim
                Dim FileBytes(FileUpload1.PostedFile.InputStream.Length) As Byte

                FileUpload1.PostedFile.InputStream.Read(FileBytes, 0, FileBytes.Length)

                Dim SQL As String = "update CorpLogo "
                SQL += "set HeaderLogoName = @LogoFile, LogoImage = @LogoBytes "
                SQL += " where isonum='100'"

                Dim myConn As New SqlConnection(connString)
                Dim SQLcmd As New SqlCommand(SQL, myConn)
                SQLcmd.Parameters.AddWithValue("@LogoFile", LogoFile)
                SQLcmd.Parameters.AddWithValue("@LogoBytes", FileBytes)

                myConn.Open()
                SQLcmd.ExecuteNonQuery()
                myConn.Close()
            End If


Just a brief note, since you have solved the problem. A Stream is like a firehose. It moves "water" from one location to another. Once you have pulled from the stream, the bits you are looking for are no longer there, as they are already pulled to a location rather than "held in the stream". In short, a stream is a bridge from one persistant location to another. Once items have cleared the stream, they are no longer there.

This is why you cannot get the bits (corruption) after you have gotten the bits.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜