Print This Post

Minor updates to the ASP.Net file upload module

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 use document.location = document.location to 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.

There Are 23 Responses So Far. »

  1. 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

  2. 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!

  3. 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

  4. 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, SendCalculatedContentLength has been around in the HTTPWorkerRequest class 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

  5. 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.

  6. 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

  7. 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-

  8. 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.

  9. 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.

  10. 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

  11. Hi Pitoo,
    Thanks for this. I’ll add the remove button property into the next version.

    Cheers,
    Darren

  12. 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

  13. 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

  14. 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.

  15. 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

  16. Oh, I’m using FireBug on Firefox to see the URL parameters and http response on the UploadProgress.ashx calls.

  17. 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?

  18. 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.

  19. 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

  20. 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.

  21. 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.

  22. 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

  23. 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();

Post a Response