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



Go to last post Go to first unread
#1 Posted : Wednesday, October 8, 2008 9:00:57 PM(UTC)

Rank: Advanced Member

Groups: Member
Joined: 9/22/2008(UTC)
Posts: 39

I know this is probably more of my coding error and not an Image Uploader issue, but I'm using it in the IU and I'm a newbie at ASP.NET VB script.

The code I have below works great...everything uploads, thumbnails specified, etc. But what I'm trying to do is no matter how many images the person uploads at once (1 or 100) there should only be one unique folder name created for that particular upload session, and then three subfolders within that (large, medium, thumbs). I've got it down so that the unique folder is created and the 3 subdirectories are created and the images of appropriate size are copied into the correct directories.

BUT, it's creating new unique directories for each image uploaded...not throwing them all into one unique directory. But the way I read this code it looks like it should be creating 1 unique directory per upload session, not per uploaded file. Can someone tell me what I'm doing wrong and what I need to do to move around my code so that it works as I want it to? Thanks

<%@ Page Language="VB" AutoEventWireup="false" ValidateRequest="false" Debug="true" %>

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>

<script runat="server">
	'This variable specifies relative path to the folder, where the gallery with uploaded files is located.
	'Do not forget about the slash in the end of the folder name.
	Dim iNumChars As Integer = CInt("6")
	Dim randDir As String = RandomString(iNumChars)
	Private galleryPath As String = "/imagegallery/images/" & Format(DateTime.Today, "MM_dd_yyyy") & "/"
	Dim newDir As String = randDir
	Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

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

		'Create the directories to upload the images too
		Directory.CreateDirectory(Server.MapPath(galleryPath & newDir & "/large"))	'original source file directory
		Directory.CreateDirectory(Server.MapPath(galleryPath & newDir & "/medium"))	'medium sized thumb directory
		Directory.CreateDirectory(Server.MapPath(galleryPath & newDir & "/thumbs"))	'small sized thumb directory
		'Iterate through uploaded data and save the original file, thumbnail, and description.
		For i As Integer = 1 To fileCount

			'Get source file and save it to disk.
			Dim sourceFile As HttpPostedFile = Request.Files("SourceFile_" & i)
			Dim theTitle As String = Request.Form("Title_" & i)
			Dim theDescription As String = Request.Form("Description_" & i)
			Dim fileName As String = GetSafeFileName(Path.GetFileName(sourceFile.fileName))
			sourceFile.SaveAs(Server.MapPath(galleryPath & newDir & "/large/" & fileName & ".jpg"))

			'Get first thumbnail (the single thumbnail in this code sample) and save it to disk.
			Dim thumbnail1File As HttpPostedFile = Request.Files("Thumbnail1_" & i)
			thumbnail1File.SaveAs(Server.MapPath(galleryPath & newDir & "/medium/" & fileName & ".jpg"))
			Dim thumbnail2File As HttpPostedFile = Request.Files("Thumbnail2_" & i)
			thumbnail2File.SaveAs(Server.MapPath(galleryPath & newDir & "/thumbs/" & fileName & ".jpg"))

			'Save file info. If you modify the script.js to send more fields, do not forget to 
			'extract appropriate variables here.
			'Dim xmlFile 		As 	XmlElement = descriptions.CreateElement("file")
'			xmlFile.SetAttribute("name", fileName)
'			xmlFile.SetAttribute("width", Request.Form("Width_" & i))
'			xmlFile.SetAttribute("height", Request.Form("Height_" & i))
'			xmlFile.SetAttribute("title", Request.Form("Title_" & i))
'			xmlFile.SetAttribute("description", Request.Form("Description_" & i))
'			descriptions.DocumentElement.AppendChild(xmlFile)

		'descriptions.Save(Server.MapPath(galleryPath & "Descriptions.xml"))
	End Sub

	'This method verifies whether file with such name already exists 
	'and if so, construct safe filename name (to avoid collision).	
Private Function GetSafeFileName(ByVal fileName As String) As String
		Dim newFileName As String = fileName
		Dim j As Integer = 1
		While (File.Exists(Server.MapPath(galleryPath & newFileName)))
			newFileName = j & "_" & fileName
			j += 1
		End While
		Return newFileName
End Function
Public Shared Function RandomString(ByVal iLength As Integer) As String

    Dim iZero, iNine, iA, iZ, iCount, iRandNum As Integer
    Dim sRandomString As String

    ' we'll need random characters, so a Random object 
    ' should probably be created...
    Dim rRandom As New Random(System.DateTime.Now.Millisecond)

    ' convert characters into their integer equivalents (their ASCII values)
    iZero = Asc("0")
    iNine = Asc("9")
    iA = Asc("A")
    iZ = Asc("Z")

    ' initialize our return string for use in the following loop
    sRandomString = ""

    ' now we loop as many times as is necessary to build the string 
    ' length we want
    While (iCount <  iLength)
        ' we fetch a random number between our high and low values
        iRandNum = rRandom.Next(iZero, iZ)

       ' here's the cool part: we inspect the value of the random number, 
       ' and if it matches one of the legal values that we've decided upon,  
       ' we convert the number to a character and add it to our string
       If (((iRandNum > = iZero) And (iRandNum <= iNine) _
            Or (iRandNum > = iA) And (iRandNum < = iZ))) Then 
	sRandomString = sRandomString + Chr(iRandNum) 
	iCount = iCount + 1 
       End If
    End While
    ' finally, our random character string should be built, so we return it
    RandomString = sRandomString 
End Function

#2 Posted : Thursday, October 9, 2008 1:00:35 AM(UTC)

Rank: Advanced Member

Groups: Member
Joined: 9/22/2008(UTC)
Posts: 39

I'm reading the documentation and I have a suspicion that this has to do with the fact that when you set for concurrent uploads:

iu.addParam("FilesPerOnePackageCount", "1");
iu.addParam("AutoRecoverMaxTriesCount", "5");

Then ImageUploader sends the first thumbnail to the upload.aspx script...then goes back and grabs the second thumbnail, runs the script again, etc...so by the end of it all if you have 5 images then I'm going to get 5 random named directories according to my script.

So, do I bump up the FilesPerOnePackageCount to a max number? What if I don't know the max number? Is that going to kill my server?

OR...is there a way to carry the directory name that was created from the first file upload and keep it the same for the entire upload process for all the rest of the images?
#3 Posted : Thursday, October 9, 2008 9:17:22 PM(UTC)

Rank: Advanced Member

Groups: Member
Joined: 9/22/2008(UTC)
Posts: 39

I figured out another way to do it. I tried using the BeforeUpload event listener and for some reason it wasn't giving me an error, but it was sending across the additional data I was trying to send across. So, I just went the route of sending over an additional form from the default.aspx page and everything worked out exactly as I needed it to. That way I'm getting a static folder name created that is sent with the form rather than having the upload script run each time an image is processed resulting in multiple folders.
#4 Posted : Sunday, October 12, 2008 7:26:21 PM(UTC)

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 8/3/2003(UTC)
Posts: 1,070

Thanks: 1 times
Was thanked: 12 time(s) in 12 post(s)

It is common approach to send special information via POST fields. So that way you can send folder name and grab it everytime server upload script is requested.

Another approach is to set FilesPerOnePackageCount to -1. In this case all the files will be uploaded in one request and server upload script will be requested once. So your idea described in the first post will work.
Sincerely yours,
Dmitry Sevostyanov

UserPostedImage Follow Aurigma on Twitter!
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.