Welcome Guest! You need to login or register to make posts.



Go to last post Go to first unread
#1 Posted : Wednesday, March 16, 2011 7:20:22 AM(UTC)

Rank: Newbie

Groups: Member
Joined: 3/15/2011(UTC)
Posts: 2


Can anyone tell me how to rename the file as it is uploaded?

I have a required field in the form called stockNumber. The image should only be uploaded if the stockNumber field is not empty and should be named the same as the user input for stockNumber. I am using asp.net 4.0 and C# and aurigma library.

Thanks for any help.

#2 Posted : Monday, March 21, 2011 7:45:42 AM(UTC)

Rank: Newbie

Groups: Member
Joined: 3/21/2011(UTC)
Posts: 3

Here is a VB.net version that seems to work:

This was put in the Page.Load event, but could be placed anywhere that has access to the request.form object

        Dim fileCount As Integer
            'Get total number of uploaded files (all files are uploaded in a single package).
            fileCount = Int32.Parse(Request.Form("FileCount"))
        Catch ex As Exception
            fileCount = 0
        End Try

        'Iterate through uploaded data and save the original file and thumbnail
        For I As Integer = 1 To fileCount
            'Get source file and save it to disk.
            Dim fileName As String = String.Empty
                Dim sourceFile As HttpPostedFile = Request.Files("SourceFile_" & I)
                fileName = System.IO.Path.GetFileName(sourceFile.FileName)
                sourceFile.SaveAs(server.mappath([i]UploadFolder[/i] & "/" & fileName))

                Do While fileName.Contains("\")
                    fileName = fileName.Substring(InStr(fileName, "\"))

                CreateThumbnail(sourceFile.InputStream, fileName)

AND THIS IS THE CREATETHUMBNAIL Code - I use my own rather than the built in one!!!

    Private Sub CreateThumbnail(ByVal Fil As System.IO.Stream, ByVal ImageID As Integer, ByVal Filename As String)
            Dim Input As New Bitmap(Fil)
            Dim useX, useY As Integer
            If Input.Height > Input.Width Then
                useY = [i]DefaultThumbHeight[/i]
                useX = Input.Width / (Input.Height / [i]DefaultThumbHeight[/i])
                useX = [i]DefaultThumbWidth[/i]
                useY = Input.Height / (Input.Width / [i]DefaultThumbWidth[/i])
            End If
            Dim output As New Bitmap(useX, useY)
            Dim graphics As Graphics = Drawing.Graphics.FromImage(output)
            graphics.SmoothingMode = Drawing2D.SmoothingMode.Default
            graphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.Default
            graphics.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
            graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            graphics.DrawImage(Input, 0, 0, useX, useY)
            Dim imageData As New System.IO.MemoryStream
            Dim Info() As System.Drawing.Imaging.ImageCodecInfo = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()
            Dim Params As System.Drawing.Imaging.EncoderParameters = New System.Drawing.Imaging.EncoderParameters(1)
            Params.Param(0) = New Imaging.EncoderParameter(Imaging.Encoder.Quality, 100L)
            output.Save(imageData, Info(1), Params)

            output.Save(Server.Mappath([i]ThumbnailUploadFolder[/i] & "/" & Filename, Imaging.ImageFormat.Jpeg)

        Catch ex As Exception
             'TODO: error handling
        End Try

    End Sub

AS YOU CAN SEE, you can change the filename at anytime through the process and set your own based on an input field or anything.....I typically use the autoincrement primary key from the database table as the filename.


Edited by moderator Saturday, April 30, 2011 3:35:44 AM(UTC)  | Reason: Not specified

Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.