SlickUpload
Welcome Guest Search | Active Topics | Log In | Register

2 Pages 12>
Upload single image and create two thumbnails Options
Peter McCoder
#1 Posted : Tuesday, August 09, 2011 12:52:54 PM
Rank: Member

Groups: Registered

Joined: 8/9/2011
Posts: 17

Hi,

I can't seem to find an example of code required to achieve the following fairly basic steps so it would be great if you could give me a hand...

A user is presented with a form on our site with 3 fields: Name, Description, Ajax SlickUpload control (limited to allowing one file only).

When the user adds an image to the SlickUpload control:

1 - The file is validated to be either JPG or PNG (error message returned if neither)

2 - The file is saved to a folder as a GUID

3 - The file is uploaded and then resized to 300 x 300 if required.

4 - A second thumbnail of 150 x 150 is created from the original file.

5 - The thumbnail image is displayed on the form beside the upload control (and also a record of the created images will be stored somewhere so that once the form is submitted, file paths can be added to the database)

 

Steps 1 and 2 seem to be covered in the sample code, but I can't find any code on how to "catch" the uploaded image and resize to make 2 thumbnails.

My company are very interested in purchasing a licence if we find it works as we require so any assistance would be very appreciated!

 

Many thanks,

Peter

Peter McCoder
#2 Posted : Tuesday, August 09, 2011 12:57:19 PM
Rank: Member

Groups: Registered

Joined: 8/9/2011
Posts: 17
Just to add ... I already have code for creating thumbnails from a file, given it's path, so I really just need an example of "how to intercept" a new upload to create associated files.

Perhaps there's a "onFileUploadComplete" function?
Axosoft
#3 Posted : Tuesday, August 09, 2011 3:48:42 PM
Rank: Administration


Groups: Administrators

Joined: 7/7/2005
Posts: 1,586
Location: Scottsdale, AZ

There is a way to do this, using a custom UploadStreamProvider that will resize the image. I'll put together a sample for you -- I should have this to you by tomorrow.

Peter McCoder
#4 Posted : Wednesday, August 10, 2011 5:31:35 AM
Rank: Member

Groups: Registered

Joined: 8/9/2011
Posts: 17

Great thank you. Will the UploadStreamProviderbe able to return an error if I throw an Exception etc? (e.g. if something goes wrong with thumbnail creation)

Peter McCoder
#5 Posted : Wednesday, August 10, 2011 11:52:11 AM
Rank: Member

Groups: Registered

Joined: 8/9/2011
Posts: 17
Hi Chris,

A few more issues. Unfortunately as I'm working under GMT I imagine I won't get a response to these until tomorrow (my time), although I'm here for another few hours :)

As mentioned in the first post, I want to limit the uploadable files to "PNG" and "JPEG/JPG".

I call the following javascript from the "OnClientFileAdded":

function onFileSelectionChanged(file) {
var slickUpload = kw("<%=slickUpload.ClientID %>");

document.getElementById("uploadButton").className = "button" + (slickUpload.get_Files().length > 0 ? "" : " disabled");

if (slickUpload.get_Files().length > 0) {

var myreg = /^([jJ][pP][gG]|[jJ][pP][eE][gG]|[pP][nN][gG])$/

if (!myreg.test(file.extension)) {

alert("Invalid file format. Only JPEG and PNG image files are allowed.");
slickUpload.remove_File(file.get_Id());
}
}
}

It seems to be failing on the "file.get_Id()" call saying that "Object doesn't support this property of method".


Incidentally, I'm using AJAX 1.0.61025 as our project is ASP.NET 2.0.

Is this an issue? Does SlickUpload require a later AJAX library?
Peter McCoder
#6 Posted : Wednesday, August 10, 2011 12:10:27 PM
Rank: Member

Groups: Registered

Joined: 8/9/2011
Posts: 17
Last question:

I'd like it to be possible to upload the file as soon as it is selected by "Add Files" (assuming it is valid), rather than having to click the "Upload" button.

(If the user clicks "Add Files" again, then the previous image (and thumbnails) will be deleted by my code)

I tried adding:

var slickUpload = kw("<%=slickUpload.ClientID %>");
slickUpload.start();

to the function called by "OnClientFileAdded", but it just reloads the page and doesn't upload the file.

Is this possible? Thanks again
Axosoft
#7 Posted : Wednesday, August 10, 2011 3:34:34 PM
Rank: Administration


Groups: Administrators

Joined: 7/7/2005
Posts: 1,586
Location: Scottsdale, AZ

Right now, I have a sample that pulls together everything except the actual display of the thumbnail. The validation, thumbnail generation, etc. are in place, but I've run into a snag -- the current SlickUpload version doesn't have support for passing information about a file back to the client, so there is no way to notify the page about which thumbnail url to use. *blush*

Can you give us a couple of days? This is a feature we have on the todo list for SlickUpload, we'll just bump it up to the front of the line -- we should have something for you by Friday, or Monday at the latest.

Peter McCoder
#8 Posted : Thursday, August 11, 2011 6:55:42 AM
Rank: Member

Groups: Registered

Joined: 8/9/2011
Posts: 17
Thanks, appreciate the help!
Axosoft
#9 Posted : Monday, August 15, 2011 3:54:46 PM
Rank: Administration


Groups: Administrators

Joined: 7/7/2005
Posts: 1,586
Location: Scottsdale, AZ

Here's the Thumbnail sample: http://krystalware.com/files/ThumbnailSample.zip. We'll be adding this across all of our supported environments eventually, but wanted to get it into your hands ASAP.

The functionality for this is in two places. On the server side, the actual thumbnailing occurs in the ThumbnailFileUploadStreamProvider. We used a dead simple implementation, which doesn't have logic to handle all of the edge cases -- feel free to swap in whatever you want to use.

The CloseWriteStream method gets called when a file has been uploaded. isComplete will be true if the file was completed or false if there was an error or disconnection. On completion, we open the file that was uploaded, thumbnail it, and save the thumbnail. We also dump the thumbUrl into the file.Data dictionary, so that information will be available client side.

The RemoveOutput method will be called if a file errors or is cancelled or removed. We override that too, to remove the thumbnail image.

Client side, we handle the onFileUploadEnded method. This gives us access to the file information, including the stuff we stuck into the file.Data dictionary. We pull out the thumbUrl and use it for the thumbnail <img />.

The theme is a bit rough, so feel free to tweak the template as you like to fit your site.

Let me know if you have any questions.

Peter McCoder
#10 Posted : Tuesday, August 16, 2011 1:27:48 PM
Rank: Member

Groups: Registered

Joined: 8/9/2011
Posts: 17
Thanks Chris, I'll give it a lash tomorrow and let you know how it goes :)
Peter McCoder
#11 Posted : Friday, August 19, 2011 9:59:28 AM
Rank: Member

Groups: Registered

Joined: 8/9/2011
Posts: 17
Hi Chris,

The Krystalware.SlickUpload.dll in the zip above doesn't seem to have CloseWriteStream() in the FileUploadStreamProvider class.

Could you repost when you get a chance please?

Thanks!
Peter
Peter McCoder
#12 Posted : Friday, August 19, 2011 10:11:33 AM
Rank: Member

Groups: Registered

Joined: 8/9/2011
Posts: 17
Checked the main download zip as well and it doesn't seem to be included in there either.

.NET2 dll is the version I'm looking at ... many thanks!
Axosoft
#13 Posted : Saturday, August 20, 2011 7:58:35 PM
Rank: Administration


Groups: Administrators

Joined: 7/7/2005
Posts: 1,586
Location: Scottsdale, AZ

Ahh. I'll get you a .NET 2.0 build tomorrow.

I apologize for the delay -- our forum post notifications appear to have flaked yesterday.

Axosoft
#14 Posted : Sunday, August 21, 2011 3:07:37 PM
Rank: Administration


Groups: Administrators

Joined: 7/7/2005
Posts: 1,586
Location: Scottsdale, AZ

You can find the .NET 2 thumbnail sample here: http://krystalware.com/files/thumbnailsample-net2.zip.

Let me know if this works for you.

Peter McCoder
#15 Posted : Wednesday, August 24, 2011 11:50:31 AM
Rank: Member

Groups: Registered

Joined: 8/9/2011
Posts: 17
Hi Chris .. just looked there. The object browser still doesn't show the CloseWriteStream() function... would you mind checking the .net2 version please?
Thanks!
Axosoft
#16 Posted : Thursday, August 25, 2011 7:20:23 AM
Rank: Administration


Groups: Administrators

Joined: 7/7/2005
Posts: 1,586
Location: Scottsdale, AZ

Are you getting a compile error with the code?

Everything looks to be correct as far as I can see. The CloseWriteStream methodis implemented in the UploadStreamProviderBase class -- the class that FileUploadStreamProvider derives from. It isn't overridden in the FileUploadStreamProvider, as FileUploadStreamProvider uses the default implementation from its base class.

By default, Object Browser doesn't show inherited members. To change this, go into the Object Browser Settings dropdown and check the "Include Inherited Members" checkbox. Once you do that, you will see the CloseWriteStream method on the FileUploadStreamProvider class.

Peter McCoder
#17 Posted : Thursday, August 25, 2011 7:32:10 AM
Rank: Member

Groups: Registered

Joined: 8/9/2011
Posts: 17
That was it, thanks .. will give it a thorough test today and let you know if any issues come up.
Peter McCoder
#18 Posted : Thursday, August 25, 2011 12:38:55 PM
Rank: Member

Groups: Registered

Joined: 8/9/2011
Posts: 17
After a bit of tweaking it works like a charm! Thanks for your help ... now the fun begins of integrating it fully into the project :)
Peter McCoder
#19 Posted : Thursday, August 25, 2011 1:14:14 PM
Rank: Member

Groups: Registered

Joined: 8/9/2011
Posts: 17
Hi Chris,

I've encountered an issue where I try to throw an Exception in the CloseWriteStream() function ...

If a file isn't an image, or there are issues resizing the files etc, I want to be able to throw an exception and output the error message by using the "status.errorMessage" javascript object.

My code for CloseWriteStream() is as follows...

Public Overrides Sub CloseWriteStream(ByVal file As UploadedFile, ByVal stream As Stream, ByVal bIsComplete As Boolean)

MyBase.CloseWriteStream(file, stream, bIsComplete)

'If file uploaded successfully...
If bIsComplete Then

'Declare variables
Dim objPhoto As New Photo
Dim sFolderPath As String = file.ServerLocation.Replace(System.IO.Path.GetFileName(file.ServerLocation), "")
Dim sFileExtension As String = System.IO.Path.GetExtension(file.ClientName) 'Get file extension of original image
Dim sFileName As String = System.IO.Path.GetFileNameWithoutExtension(file.ServerLocation) 'Get filename (without extension) of image stored on server
Dim sFileName_Thumb As String = String.Format("{0}_t", sFileName)
Dim sFileSrc As String = String.Format("{0}{1}", sFileName, sFileExtension)

'Throw an exception if file extension is invalid
If sFileExtension = "" Then Throw New Exception(String.Format("File name '{0}' is invalid. All files must have a valid extension. (e.g. '.jpg')", file.ClientName))

'Throw an exception if the file type is invalid
Select Case LCase(sFileExtension)
Case ".jpg", ".png", ".jpeg"
'do nothing as file is valid
Case Else
'Delete original file
FileUtil.Delete(file.ServerLocation)
Throw New Exception("File type is invalid. Valid file types include: jpg and png only")
End Select

'Rename file to add file extension if required
Try
FileUtil.Rename(file.ServerLocation, sFolderPath & sFileSrc)
Catch ex As Exception
'Delete original file
FileUtil.Delete(file.ServerLocation)
Throw New Exception(String.Format("Problem storing source image: {0}", ex.Message))
End Try

'Resize file if required
Try
objPhoto.ResizeImage(sFolderPath & sFileSrc, sFolderPath & sFileSrc, 350, 350, 85&, False)
Catch ex As Exception
'Delete original file
FileUtil.Delete(sFolderPath & sFileSrc)
Throw New Exception(String.Format("Problem resizing source image: {0}", ex.Message))
End Try

'Create thumbnail
Try
objPhoto.ResizeImage(sFolderPath & sFileSrc, String.Format("{0}{1}{2}", sFolderPath, sFileName_Thumb, sFileExtension), 80, 80, 75&, False)
Catch ex As Exception
'Delete original file and thumbnail file if there was an error saving thumbnail
FileUtil.Delete(sFolderPath & sFileSrc)
FileUtil.Delete(String.Format("{0}{1}{2}", sFolderPath, sFileName_Thumb, sFileExtension))
Throw New Exception(String.Format("Problem creating thumbnail: {0}", ex.Message))
End Try

'Pass file data back to SlickUpload
file.Data("imageFile") = sFileSrc
file.Data("thumbFile") = String.Format("{0}{1}", sFileName_Thumb, sFileExtension)
End If
End Sub


The odd thing is .. this works perfectly fine when I am in Debug mode, but doesn't work when running the site in standard mode. If I upload an invalid file in standard mode, the upload progress bar just remains static on screen at 0%.

Do you know are there any issues with throwing exceptions in CloseWriteStream() ? Or is there a better way to handle these errors in the function?

Thanks again
Peter McCoder
#20 Posted : Thursday, August 25, 2011 2:09:29 PM
Rank: Member

Groups: Registered

Joined: 8/9/2011
Posts: 17
Chris, sorry one more question to add to the previous, but relating to a different section ...

On the main form I have <asp:TextBox> inputs as well as the image uploader.

Thus I have a main "submit form" button at the bottom of the form.

What is happening now is that I need to click the button TWICE in order to submit the form.

On the first click the form doesn't postback but instead the "slickUpload_selector" style is set to "display: none;" and the following hidden controls are added to the page:

<input type="hidden" name="kw_uploadSessionId" value="7D963ECC-3ECE-4B20-B6A8-ED5D60279E21">
<input type="hidden" name="kw_uploadErrorType" value="Cancelled">

On the second click, the form submits as normal.

This can possibly be replicated on your side by adding a button to the Thumbnail/Default.aspx page on your sample project.

Any ideas?
Users browsing this topic
Guest
2 Pages 12>
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.