ASP Upload

Discussion in 'ASP / ASP.net' started by sladmin, Jun 22, 2009.

  1. sladmin CSNM Reseller

    Hi guys,

    I'm just working on an upload section of a particular website at the moment and would like to check that the file extension is correct before upload. However, without using ajax I have to use more than one page with if statements to make it work.

    My question however, is can I delay the upload but save the data in variables, or do I HAVE to upload the file firstly, check it properties and then delete it should it be wrong?

    I just think it might lag the server if everytime someone uploads a file (whether the correct files or not) and then either delete them using the System File Object or insert the file information into the database.

    Any ideas because I've tried delaying the upload file until after the check pages, but it doesn't seem as if you can pass variables into a file field.

    Ohh and the files will never be any large than about 5mb each.

    Many thanks,
    Adam
  2. siphilp CSNM Customer

    You could always put an onchange event on the file textbox, put it through a regular expression that grabs the file extension and if it doesn't match then present user with a error but that would rely on javascript being enabled...

    You could do the check server side before saving the file (asp.net) :S

    Code:
    Uploader = asp:FileUpload
     
    [SIZE=2]var sFileExt = Functions.Files.ReturnFileExtension(Uploader.PostedFile.FileName.Trim());[/SIZE]
     
    [SIZE=2][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](Functions.Files.CheckForAllowedTypes(sFileExt)[/SIZE][SIZE=2])[/SIZE]
    [SIZE=2]{[/SIZE]
    var FileObjectDetails = new FileObjectDetails(size,ext,type,name,guid);
    [INDENT][SIZE=2]if(Business.AddFileDetails(FileObjectDetails))[/SIZE]
    {
    [SIZE=2]Uploader.PostedFile.SaveAs(Server location);[/SIZE]
    }else
    {
    //problem saving details to db
    }
    [/INDENT][SIZE=2]}else[/SIZE]
    [SIZE=2]{[/SIZE]
    [SIZE=2]lblMessage.Text = "Wrong File Type";[/SIZE]
    [SIZE=2]}[/SIZE]
    [/SIZE]
    
  3. siphilp CSNM Customer

  4. Ben Collier CSNM Customer

  5. diego CSNM Customer

  6. diego CSNM Customer

    PS.: you can't pass variables along with the file field but you CAN pass variables in the query string.

    Basically, you can POST the binary data (the files) to an URL with a query string, like this:
    /upload.asp?hello=1&world=2
  7. sladmin CSNM Reseller

    Hi Diego,

    Unfortunately in order to use the querystring method you will need to use a request.querystring to call the information. Anything to do with a request method is not allowed since the upload method used interfers with this.

    I got around this by using a session variable across two pages, which then went onto the upload page.

    Thanks for the help though guys :)
  8. Mark Voss CSNM Customer

    Thanks for this info Adam, just needed to do something that needed variables passing between a form and an ASPUpload script and found this thread ;-)
  9. sladmin CSNM Reseller

    No problem! Glad I could help in some way :)

Share This Page