This is a migrated thread and some comments may be shown as answers.

proble radupload and database

4 Answers 98 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
carlos perez carlos
Top achievements
Rank 1
carlos perez carlos asked on 01 Jul 2011, 07:41 PM
Good morning, I'm trying to change my old code to upload images. But it hascomplicated everything. I can not find how to adapt what I have to control radupload. Help me please.

Example
In this part of code

Public Sub subAddNewPicture(ByVal FileUploader As FileUpload, ByVal strPictureName AsStringByVal strImgFolder As String)

        Dim strImageFolderPath As String

        Dim strImagePath As String

        Try

            '   Construct saving path

            strImageFolderPath = Path.Combine(Request.PhysicalApplicationPath, strImgFolder)

            strImagePath = Path.Combine(strImageFolderPath, strPictureName)

            '   Upload image this part is not as fit

            FileUploader.PostedFile.SaveAs(strImagePath)

            FileUploader.PostedFile.InputStream.Dispose()

            FileUploader.Dispose()

        Catch ex As Exception

            'Throw ex

        End Try

    End Sub



All code here

Protected Sub Button1_Click(ByVal sender As ObjectByVal e As System.EventArgs) HandlesButton2.Click

        Dim tamano_fotoP As Integer

        Dim tamano_fotoG As Integer

        tamano_fotoP = 235

        tamano_fotoG = 649

        Try

            Dim strGuid As String = ""

            Dim strPicExtension As String = ""

            Dim strFileName As String = ""

            If PhotoUpload.HasFile = True Then

                strGuid = fnGuid()

                strPicExtension = fnGetPictureExtension(PhotoUpload.FileName)

                strFileName = strGuid & strPicExtension

                txtfoto.Text = strFileName

                If strPicExtension <> "none" Then

                    subAddNewPicture(PhotoUpload, strFileName, "Imagenes")

                    Resize_Image(tamano_fotoP, tamano_fotoP, strFileName, "~/Imagenes/","P/")

                    Resize_Image(tamano_fotoG, tamano_fotoG, strFileName, "~/Imagenes/","G/")

                    sqldata.Insert()

                    PhotoUpload.PostedFile.InputStream.Dispose()

                    PhotoUpload.Dispose()

                    GC.Collect()

                    eliminar_foto(strFileName, "")

                Else

                    lblmensaje.Text = "Por favor seleccione una imagen..."

                End If

            Else

                lblmensaje.Text = "Por favor seleccione una imagen..."

            End If

            lblmensaje.Visible = True

        Catch ex As Exception

        End Try

    End Sub

    Protected Sub eliminar_temporales()

        Dim file() As FileInfo

        Dim i As Integer

        Dim txtpath As String

        txtpath = Path.Combine(Request.PhysicalApplicationPath, "Imagenes")

        If txtpath <> "" Then

            Dim dir As New DirectoryInfo(txtpath)

            file = dir.GetFiles()

            If file.Length > 0 Then

                For i = 0 To file.Length - 1

                    Try

                        file(i).Delete()

                    Catch ex As Exception

                        'Throw ex

                    End Try

                Next

            End If

        End If

    End Sub

    Public Sub eliminar_foto(ByVal nombre_foto As StringByVal folder As String)

        Dim strImagePath, strImageFolderPath As String

        Try

            strImageFolderPath = Path.Combine(Request.PhysicalApplicationPath, "Imagenes"& folder)

            strImagePath = Path.Combine(strImageFolderPath, nombre_foto)

            System.IO.File.Delete(strImagePath)

        Catch ex As Exception

            'Throw ex

        End Try

    End Sub

    Public Sub Resize_Image(ByVal intMaxWidth As ShortByVal intMaxHeigth As ShortByValfoto As StringByVal folder As StringByVal atributo As String)

        Dim img As Image

        img = System.Drawing.Image.FromFile(MapPath(folder & foto))

        Dim oImg As Bitmap = New System.Drawing.Bitmap(img)

        'Assign image dimensions

        Dim oldWidth As Integer

        Dim oldHeight As Integer

        Dim newWidth As Integer

        Dim newHeight As Integer

        Dim ratio As Decimal

        oldWidth = oImg.Width

        oldHeight = oImg.Height

        ratio = oldWidth / oldHeight

        newWidth = intMaxWidth

        newHeight = newWidth / ratio

        'rutina para ver si la foto es mas pequena del tamano a recortar dejandola igual

        If oImg.Width < intMaxWidth Then

            newWidth = oImg.Width

            newHeight = oImg.Height

        End If

        Dim OutputBitmap As Bitmap = New Bitmap(oImg, CInt(newWidth), CInt(newHeight))

        Dim encoderParams As System.Drawing.Imaging.EncoderParameters = NewSystem.Drawing.Imaging.EncoderParameters()

        Dim quality As Long

        Dim imageFormat = img.RawFormat

        Dim myresizer As Graphics

        myresizer = Graphics.FromImage(OutputBitmap)

        'myresizer.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality

        'myresizer.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality

        myresizer.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic

        myresizer.DrawImage(img, 0, 0, newWidth, newHeight)

        'Compress image

        Dim comp As Integer

        If atributo = "G/" Then

            comp = 60

        Else

            comp = 70

        End If

        quality = comp ' 0 to 100 - 100 highest quality

        Dim encoderParam As System.Drawing.Imaging.EncoderParameter = NewSystem.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality)

        encoderParams.Param(0) = encoderParam

        Dim arrayICI As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()

        Dim jpegICI As ImageCodecInfo

        Dim x As Integer

        For x = 0 To arrayICI.Length - 1

            If (arrayICI(x).FormatDescription.Equals("JPEG")) Then

                jpegICI = arrayICI(x)

                Exit For

            End If

        Next

        'Save original image

        If Not jpegICI Is Nothing Then

            OutputBitmap.Save(Server.MapPath(folder) & atributo & foto, jpegICI, encoderParams)

        End If

        '  clean up

        OutputBitmap.Dispose()

    End Sub

#Region "Public Functions"

    '   Adds picture to specified folder

   

Public Sub subAddNewPicture(ByVal FileUploader As FileUpload, ByVal strPictureName AsStringByVal strImgFolder As String)

        Dim strImageFolderPath As String

        Dim strImagePath As String

        Try

            '   Construct saving path

            strImageFolderPath = Path.Combine(Request.PhysicalApplicationPath, strImgFolder)

            strImagePath = Path.Combine(strImageFolderPath, strPictureName)

            '   Upload image ACTUALIZADO

            FileUploader.PostedFile.SaveAs(strImagePath)

            FileUploader.PostedFile.InputStream.Dispose()

            FileUploader.Dispose()

        Catch ex As Exception

            'Throw ex

        End Try

    End Sub

    '   Function to generate a GUID using current date and time

    Public Function fnGuid() As String

        Dim strGuid As String = ""

        Try

            With Today

                strGuid = .Year & .Month & .Day & Now.Hour & Now.Minute & Now.Second

            End With

        Catch ex As Exception

            Throw ex

        End Try

        '   returning the guid generated

        Return strGuid

    End Function

    '   Returns true if file name has the extension .jpg, .gif, .jpeg

    Public Function fnGetPictureExtension(ByVal strPictureName As StringAs String

        Try

            With strPictureName.ToUpper

                If .EndsWith(".JPG"Then

                    Return ".JPG"

                ElseIf .EndsWith(".GIF"Then

                    Return ".GIF"

                ElseIf .EndsWith(".JPEG"Then

                    Return ".JPEG"

                ElseIf .EndsWith(".PNG"Then

                    Return ".PNG"

                ElseIf .EndsWith(".BMP"Then

                    Return ".BMP"

                End If

            End With

        Catch ex As Exception

            Throw ex

        End Try

        ' Else if has no extension so it returns ""

        Return "none"

    End Function

#End Region


4 Answers, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 2
answered on 05 Jul 2011, 01:45 PM
Hello Carlos,

You would replace:

FileUploader.PostedFile.SaveAs(strImagePath)

with

FileUploader.UploadFiles(0).SaveAs(strImagePath)

This will pull the first uploaded file in the RadUpload's UploadFiles collection

I hope that helps.
0
Kevin
Top achievements
Rank 2
answered on 05 Jul 2011, 01:52 PM
As another note, if you're using RadUpload you can set the AllowedFileExtensions property to only allow certain file types to be considered valid by RadUpload and perform the validation on the client-side and server-side using the CustomValidator control.

You can refer to this help topic for more details: http://www.telerik.com/help/aspnet-ajax/upload-client-side-validation.html
You can refer this help topic on how to make the RadUpload require the user to select a file and make sure it's one of the valid extensions: http://www.telerik.com/help/aspnet-ajax/upload-required-field-validation.html

That should help simplify your validation, instead of doing it all in the code-behind.
0
carlos perez carlos
Top achievements
Rank 1
answered on 05 Jul 2011, 03:33 PM
hi thanks for write me, i send the proyect in this ticket support. Check please and tell me, that do.
http://www.telerik.com/account/support-tickets/view-ticket.aspx?threadid=440490
0
Dimitar Terziev
Telerik team
answered on 08 Jul 2011, 04:50 PM
Hi Carlos,

As Kevin has pointed, you should use the UploadedFiles collection where the valid uploaded files are stored.

Please refer to the following help article giving information how you could manipulate the uploaded files.

Greetings,
Dimitar Terziev
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
Upload (Obsolete)
Asked by
carlos perez carlos
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 2
carlos perez carlos
Top achievements
Rank 1
Dimitar Terziev
Telerik team
Share this question
or