views:

551

answers:

4

Hi everyone, maybe a simple question but I really don't know what to do.

When I submit a file through a form using , 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!

Please help me:

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

EDIT - EDIT -EDIT

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;

}
+3  A: 

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.

routeNpingme
Ok I checked that yesterday and this probably make sense but I must do some testing and it's not that easy to do testing on prod.As soon as my bug will be correct, I will come back and mark your answer as THE answer. Thanks a lot
DarkJaff
+1  A: 

string file_ext = m.Groups[2].Captures[0].ToString(); string filename = m.Groups[1].Captures[0].ToString();

Your code presumes the groups exist. For some reason (honestly I have no regex-fu) you aren't getting the groups you think should always exist on production. I'd make sure here is an m, there is m.Groups and m.Groups.Count >= 2 and m.Groups[] has captures before calling those methods.

Wyatt Barnett
Your answer suggest the same thing as routeNpingme so I guess this is the thing I need to check. Will do in the next days. Thanks!
DarkJaff
A: 

Hi even i had the same issue.I have resolved it .Please check the below link.

http://stackoverflow.com/questions/1143834/fileupload1-postedfile-filename-is-throwing-exception

Jebli
I will look at this shortly. Thanks!
DarkJaff
A: 

Maybe the production environment places the files in a different location. Try to check what folder the production server places his files and where the development does. These issues frequently happen when the development and live are using different operating systems or different IIS versions. Possibly the difference in storage location causes your reg-ex to fail. I am not a reg-ex pro so I don't know whether your regex might contain an error, but this is the first thing that I could think of.

Also, is DEV your own machine? In that case: are you using IIS or are you using the ASP.NET development server? Because IIS and the ASP.NET development server integrated in Visual Studio behave different in certain situations.

Also: directly "jumping into" an array location is considered bad-practice by many developers (I also think it is not very good to just jump into the array assuming the correct amount of items are there). Especially when using multi dimension arrays it can get tricky when errors occur. I have seen many complex code failing on array indexes and because there were no checks it was quite hard to debug them (I am talking about 5 or 6 dimension arrays).

Gertjan