Rank: Newbie
Groups: Member
Joined: 3/15/2011(UTC) Posts: 2
|
Hi,
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.
Jerry
|
|
|
|
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 Code: Dim fileCount As Integer
Try
'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
Try
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, "\"))
Loop
CreateThumbnail(sourceFile.InputStream, fileName)
Next
AND THIS IS THE CREATETHUMBNAIL Code - I use my own rather than the built in one!!! Code: Private Sub CreateThumbnail(ByVal Fil As System.IO.Stream, ByVal ImageID As Integer, ByVal Filename As String)
Try
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])
Else
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)
graphics.Flush()
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. Clint Edited by moderator Saturday, April 30, 2011 3:35:44 AM(UTC)
| Reason: Not specified
|
|
|
|
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.