Ron's Sandbox

File Uploads

Wednesday, 21 May 2008 09:16 by hagermanr

I’m working on a project where the end user will be able to upload a comma delimited file and work will be done for each row in the file.

The file upload control works well for this but I found that it will allow the end user to upload files other than CSV files so I had to find a way around that.

There are two viable options here and they both work well so it comes down to personal preference. I personally went with option number two.

The first option is code within the submit button.

string sFileName = csvUpload.FileName.ToLower().ToString();

if(sFileName.Substring(sFileName.Length-3,3) !="csv")
    lblMsgs.Text = "This tool can only import csv files.";
else

This basically states, take the file name,  convert it to lower case and look at the last three characters. If they are not “csv” then give an error otherwise, run code to upload the file.
Option number two is to use a regular expression control in the web page itself. This looks like this:
<asp:RegularExpressionValidator
      ID="FileUploadValidator"
      runat="server"
      ErrorMessage="This tool can only process csv files."
      ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.csv|.CSV)$"
      ControlToValidate="csvUpload"
/>

 

This uses a complicated regular expression to determine that the last three characters of your file name (the file extention) is csv or CSV. If it is not then the control renders an error message instead of the code handling it. This eliminates the need for a label control to display the error in option 1 above. This method also validates the legitimacy of the file name in case the end user hand typed the path to the file. It ensures the user types a drive letter so it rules out UNC paths. (^(([a-zA-Z]:) states must begin with a-z or A-Z and a : must follow the first letter.) If you want to allow UNC paths here, you will need to remove that check for the drive letters. Option 1 would probably be much better for you if you aren’t too worried about users not using the “Browse” button or you want to allow UNC paths. You can always use a try statement to catch any errors created by a non-existent file or file path.

Now that we have the control set up and we have the file name filtered, let’s discuss what to do with it.

The file upload control has a SaveAs() function. The first thing I did was to get the drive path to the tmp directory on the web server where I want to save the file. You can hard code this value if you want to write to a remote share but the web servers anonymous user account will need write access to that share.

To get the physical path, I did a request on a server variable:

string sPath = Request.ServerVariables["APPL_PHYSICAL_PATH"].ToString();

Once I had that, I appended the \tmp path to it in the SaveAs call.

csvUpload.SaveAs(sPath + @"\tmp\" + csvUpload.FileName);

 

Here is the full else block. You can disregard the last line since it just changes the view on the web page.

else
{
      string sPath = Request.ServerVariables["APPL_PHYSICAL_PATH"].ToString();
      csvUpload.SaveAs(sPath + @"\tmp\" + csvUpload.FileName);
      mvPartners.ActiveViewIndex = (int)eViews.vwAdd;
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   C Sharp
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading