Minor updates to the ASP.Net file upload module
September 18th, 2008 • Related • Filed Under
I’ve had a few requests for updates to the file upload module. These were mainly to fix some javascript issues. In all, the updates have brought the module up to version 2.0.3, which I have now made available for download.
The changes are:
- BUG FIX: when uploads are cancelled or prevented on client side due to being too large the page is refreshed via script as this seems to be the only way to cancel the upload. Previously this was done using
location.reload. However, this method resulted in a warning message on the browser and possible re-submission of the form content. I have changed the script to usedocument.location = document.locationto refresh the page- this does not repost the contents or produce a warning. - BUG FIX: I have now updated the script so that the presence of at least one file to upload is verified before the progress bar is displayed. This prevents the bar being displayed when their are no uploads and allows other postbacks on the form.
Hope this helps and thanks again to those who continue to use the module and provide feedback.

Comment by Rob on 22 September 2008:
I don’t know if I am overlooking something or not. When I try to remove the SQL functions from Upload 2, they seems to stick. Everything I try to do, it still trys to use the SQL connections to perform the upload. Am I missing something? I can use any number 1 & 3-8 and everything is fine. When I use “DJFileUpload2″, it goes to SQL connection. I tried changing the Processor type from SQL to FileProcessor it still tried SQL. Any suggestions or am I missing something. When I try removing 2 and 3 and just using the 1st Upload(while removing the SQL processor for 2 in the code behind), then I get an Object error. Maybe something hard coded?
Thanks for the work on this excellent tool. Saved me some major bucks on my little project.
Rob
Comment by Rob on 22 September 2008:
Arggg… The References were on your build of FileUploadV2. I hate when I overlook something so simple.
Thanks for sharing this with everyone… Very Nice!
Comment by ram on 23 September 2008:
darren,
it is nice control for upload.
iam usins this control from version 1 on wards.
recently i upgrade to version 2.0.3.
in our system, i don’ have dotnet 2008, so iam rebuilding the solution in dotnet 2005 . iam getting following error
‘darrenjohnstone.net.FileUpload.UploadWorkerRequest.SendCalculatedContentLength(long)’: no suitable method found to override in UploadWorkerRequest.cs file.
can i comment this method as same method is not there in dotnet 2005?
thanks in advance.
from
ram
Comment by darren on 23 September 2008:
Hi Ram,
There’s no reason why this should be happening. Although the solution is built in VS 2008 it is targetted and designed for .Net 2.0. Also,
SendCalculatedContentLengthhas been around in theHTTPWorkerRequestclass since at least v1.1 of the framework- so something else must be wrong (http://msdn.microsoft.com/en-us/library/kfwh96kx(VS.71).aspx).You should check your references etc. and ensure that you are building correctly.
Cheers,
Darren
Comment by Chuck on 24 September 2008:
Hello! Thanks so much for this great tool!
I am having a problem and cannot find a help area on your site. I apologize if I am overlooking something. Perhaps you can e-mail me if this is not the right forum for technical issues.
I have a shared site on GoDaddy.com. I cannot install the library on the server so I have to reference it from the bin folder in the download you have provided. However, I am getting a security exception when the web.config tries to read it. I think this is the file is in full trust, like you said, but GoDaddy.com runs has a medium trust environment.
Is there any work around for this, or will I not be able to use your upload module? If I can’t, can you recommend something I could use? I have searched the web for weeks looking for help in this area, and have not found much. Your solution is by far the best one out there, so I am hoping you can provide me some help in this area.
I mainly needed a progress bar because the users will be uploading up to 200mb files and it’s just to long of a wait without giving the user some type of information. However, the entire package here is really nice, and if I can get this working I will be switching completely to your module.
Either way, thanks so much for providing such a nice solution.
Comment by darren on 24 September 2008:
Hi Chuck,
Unfortunately the module will only work with medium trust if the DLL is installed in the GAC. This is a security precaution imposed by the framework and there is no workaround.
I can’t think of another upload module which doesn’t have a similar restriction. Most rely on doing something which requires trust. If I come across anything though I will let you know.
Cheers,
Darren
Comment by Rob on 13 October 2008:
Hi Darren, a while back a couple of people including myself were asking about getting your control to work in DNN. I’m wondering if you (or someone else) has figured out how to get the control working in DNN?
Thanks-
Comment by Pitoo on 16 October 2008:
Hi Darren,
First of all, thank you very much for your great component, The flexibility is amazing.
I haven’t finished to configure it at all but (if I’m not wrong) there’s atm no Property to hide the “Remove” Button (you can hide the add button and co.) I’ve added the property myself but I think it could be good if you integrate it in your next version.
Regards,
Pit.
Comment by Javier on 19 October 2008:
Hi,
Thanks for writing this excellent tool! I’ve successfully included this into my code but I’m having a problem which I was hoping someone could help me with.
I have a form with other fields as well as this upload facility and I need to try and ensure that if the form didn’t pass the validation tests then the file isn’t uploaded. I have tried altering the IFProcessor code (startNewFile method) to include some tests on the form but can’t get it to work (i’m new to .net). Has anyone got any ideas on how I can only upload files if the server has validated the form (bad users will try playing with the form with script disabled).
Secondly, does anyone know if its possible to prevent someone with script disabled to upload a file with an invalid file extension (.exe)?
Many thanks for your help.
Comment by darren on 21 October 2008:
Hi Rob,
I’ve got some DNN code ready to make into a module. The control itself does work although some things need to be taken into account for DNN. I’ll be publishing the module soon and will send you a mail when I do.
Cheers,
Darren
Comment by darren on 21 October 2008:
Hi Pitoo,
Thanks for this. I’ll add the remove button property into the next version.
Cheers,
Darren
Comment by darren on 21 October 2008:
Hi Javier,
You’ve got the right idea with the StartNewFile method. All you really need to do is throw an exception if the file extension is invalid. The next version of the module will have server side file extension checking built in by the way- this should be out in the next few weeks.
Cheers,
Darren
Comment by Gjalt Wijma on 24 October 2008:
Hello Darren,
Thanks for writing this great code. We’ve incorporated te code in our CMS. I found a design issue with the following code:
public class UploadModule : IHttpModule
{
public UploadStatus Status
{
get { return _status; }
}
There will be one module object instance that is shared by
all requests. Putting the _status instance field on this single
module instance is not a good idea. Problems will come with
almost simultaneous uploads.
I have refactored the code and made the status field a property of the formstream. As there is one formstream instance for each upload request, this is a better place.
I have added the following code:
// Prevent browser caching
response.Cache.SetCacheability( HttpCacheability.NoCache );
// Flush
response.Flush();
// End
response.End();
to the ProcessRequest method in the UploadProgressHandler class.
This prevents browser caching of the progress.
I have added Crc32 computation to the upload:
public void Write( byte[] buffer, int offset, int count )
{
if ( _errorState ) return;
try
{
_fs.Write( buffer, offset, count );
_crc32.Update( buffer, offset, count );
}
catch ( Exception )
{
_errorState = true;
throw;
}
}
I use the crc32 class from ICSharpCode.SharpZipLib.Checksums.Crc32.
I store the crc32 in a database, an later on i use this crc to
check if the uploaded file already exists on the server.
Furthermore i’ve expanded the number of properties on the UploadedFile:
public UploadedFile( FileSystemProcessor processor )
{
// Get upload properties
ClientName = processor.GetFileName();
ClientPath = processor.GetClientFilePath();
ContentDisposition = processor.GetContentDisposition();
ContentType = processor.GetContentType();
FieldName = processor.GetFieldName();
Name = processor.GetFileName();
PhysicalPath = processor.GetPhysicalFilePath();
PhysicalTempPath = PhysicalPath;
Path = PhysicalPath.Substring( LynkxSystem.PhysicalApplicationPath.Length + 1 ).Replace( ‘\\’, ‘/’ );
Crc32 = processor.GetCrc32();
// Get file info
var fileInfo = new FileInfo( PhysicalPath );
if ( fileInfo.Exists )
ContentLength = fileInfo.Length.ToString();
}
Lastly i have added a cleanup event:
static void Context_EndRequest( object sender, EventArgs e )
{
// Check for upload status
var app = sender as HttpApplication;
// Remove status if present
var status = UploadManager.RemoveStatus( app.Context );
// Delete unsaved uploads
if ( status != null )
status.DeleteUnsavedUploads();
}
I hope that this feedback helps improve the code. I’ve heavily
refactored your code. If you want it, please send me an email.
Regards,
Gjalt Wijma
Comment by Yusuf on 15 January 2009:
Darren
Thank you for sharing such great tool. It just works great.
I added the file upload control to my form and it works great. But there are couple of instances where the upload starts when it should not have. In my form if CANCEL is clicked and a file was selected, the file gets uploaded. Also, the file upload starts on any server postback events, such as button events on the form.
Is there a way to let the upload start only when the form is ready, such as only on submit button clicked?
Thanks for the great work.
Comment by Jim on 6 February 2009:
Darren,
I’m having a couple issues.
First I get a JavaScript error using MSIE7 in the fileupload.js file up_BeginUpload method on the first if statement of “if (!Page_IsValid) return;”. Debugging says “Page_IsValid” is not defined. So I added a local JavaScript script to assign “var Page_IsValid=true;” and that got around that for the time being.
Second, I’m using the DJ FileUpload control and when I click “upload”, I see the AJAX progress bar, but it never uploads a file. I checked the UploadProgress.ashx and the contents is always “empty”. None of the files get loaded and the progress remains at 0. If I disable the use of the scripts (just rename upload_scripts dir), then the files get uploaded, but the progress bar doesn’t work. So I know the file upload process works, but there’s something else in the scripts that’s not working and not throwing any exceptions/errors that I can find.
thanks
Comment by Jim on 6 February 2009:
Oh, I’m using FireBug on Firefox to see the URL parameters and http response on the UploadProgress.ashx calls.
Comment by joerod on 6 February 2009:
Nice pieces of code. I need to store to SQL additional file related attributes like a user file description. The file name and content type came from the httprequest. Then how can I add additional element to the control and get those attribute within the list?
Comment by Jim on 14 February 2009:
I think I figured out my problems. I had the controls inside of an UpdatePanel, since I had my own AJAX based ‘wizard’ using views and multiview controls. The file upload was one view that was a couple steps into the ‘wizard’. I removed all of the steps to individual pages and the upload works fine now.
Comment by darren on 15 February 2009:
Hi Jim,
You’ll can use hidden fields for this and then access them from the PreviousFields collection. See here http://darrenjohnstone.net/aspnet-file-uploaddownload-module-v2-documentation/. Cheers
Comment by Marco Alves on 18 February 2009:
Good Component. Thx.
I am new using your module and I already have a doubt.
How might I add metadata to the uploaded file, for example, I wan’t to add a description for the file and store it in the database by myself.
Best regards,
Marco Alves.
Comment by Marco Alves on 18 February 2009:
Good Module. Thx.
I am new using your module and I already have a doubt.
How might I add metadata to the uploaded file, for example, I wan’t to add a description for each file being uploaded (I want to upload many files at a time) for the file and store it in the database by myself.
Best regards,
Marco Alves.
Comment by Marco Alves on 25 February 2009:
I changed the control so that I can add any type of control for each file being uploaded. Validation is also possible. Not just to the whole form, but as i said, for each file.
If anyone wants it, mail me marco2250@gmail.com
Comment by Joy on 4 March 2009:
Hi Darren
I have a problem of implementing the upload module with the Master-Content page. If I have the Upload Controller and Progress Bar inside a content place holder as followed:
Then I get the error message at up_killProgress function says that “;” is expected. Error is at the line:
up_killProgress(’ctl00_MainContentPlaceHolder_DJAccessibleProgrssBar2′)Sys.Application.initialize();