R – Issues with upload on production server but not in dev

asp.netfile uploadproduction

Maybe a simple question but I really don't know what to do.

When I submit a file through a form using <asp:FileUpload>, it works perfectly on my dev machine.

When I try the same thing on the server, it gives me the error below. The error doesn't help me at all because I don't even have this function in my code
(CaptureCollection) and I don't have a variable called "i". So right now, I really don't know.

Is this a question of right on the server (I don't think so because I give all the rights possible and the error is still there), is it something on my code (but it work on my dev machine…). I can show more code if you need!

The error:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Specified argument was out of the range of valid values.
Parameter name: i 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: i

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: i]
   System.Text.RegularExpressions.CaptureCollection.GetCapture(Int32 i) +5227599
   System.Text.RegularExpressions.CaptureCollection.get_Item(Int32 i) +4
   CreatePost.btnFinish_Click(Object sender, EventArgs e) +143
   System.EventHandler.Invoke(Object sender, EventArgs e) +0
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

Here is the code that does the uploading. And maybe you are right with the regex. But why is it working on dev and not on prod?

protected void btnFinish_Click(object sender, EventArgs e)
{
    string file = "";
    string csFinalPath = "";

    if (uploadPhoto.HasFile)
    {
        string filepath = uploadPhoto.PostedFile.FileName;
        string pat = @"\\(?:.+)\\(.+)\.(.+)";
        Regex r = new Regex(pat);

        //run
        Match m = r.Match(filepath);
        string file_ext = m.Groups[2].Captures[0].ToString();
        string filename = m.Groups[1].Captures[0].ToString();
        file = filename + "." + file_ext;

        //save the file to the server 
        uploadPhoto.PostedFile.SaveAs(Server.MapPath(".\\upload\\") + file);

        ThumbnailGenerator thumbGenerator = new ThumbnailGenerator();

        if (thumbGenerator.GetThumbnail(Server.MapPath(".\\upload\\") + file,
        Server.MapPath(".\\upload\\thumb\\") + "Thumb" + file))
        {
            csFinalPath = "./upload/thumb/" + "Thumb" + file;
        }
        else
        {
            //TODO: Do an error message!!!
        }
    }
    else
    {
        csFinalPath = "./images/no_image.gif";
    }

    m_database.InsertPost(Convert.ToInt32(Session["ID"].ToString()),
        Convert.ToInt32(ddlCategory.SelectedValue),
        m_nType,
        txtLink.Text,
        txtTitreFR.Text,
        txtTitreEN.Text,
        txtDescriptionFR.Text,
        txtDescriptionEN.Text,
        csFinalPath,
        "",
        "");

    panelLink.Visible = false;
    panelResult.Visible = true;

}

Best Answer

You'll need to post your code, but for a shot in the dark...

In your btnFinish_Click method on your page, there's something wrong with where you're trying to use a regular expression.

Most likely you've captured a groups of RegEx matches and tried to enumerate through them, when there really aren't any. (Or you have a For loop going through more items than the collection/list actually has.)

Edit: I'm 99% sure it's right after here:

Match m = r.Match(filepath);

Before you do anything else, after this line, check to see if there are any groups.

if (m.Groups.Count == 0) { DoSomethingElseHere(); }

Then, see if there are any captures in that group:

if (m.Groups[0].Captures.Count == 0) { DoSomethingElseHere(); }

Ultimately you'll find out what's going wrong with the input by doing this, but looking at code and not actively debugging this, this is the only good way to find out.

Edit 2: By the way, the reason in principle that you're having this issue is because you haven't really validated the input before trying to use it. The code that I just gave as a sample will get you started, but you should always sanitize what's coming to you.

Also, if you're using an upload control, not all browsers will pass in a full UNC path to the file (i.e. \server\share\file.ext) - some will just pass in the file name by itself, so these are some things to check for.