在生产服务器上上传有问题,但在开发服务器上没有上传问题

发布于 2024-07-29 22:44:41 字数 3626 浏览 9 评论 0原文

也许是一个简单的问题,但我真的不知道该怎么办。

当我使用 通过表单提交文件时,它在我的开发计算机上完美运行。

当我在服务器上尝试相同的操作时,它给出了以下错误。 该错误对我根本没有帮助,因为我的代码中甚至没有这个函数 (CaptureCollection)并且我没有名为“i”的变量。 所以现在我真的不知道。

这是服务器上的权利问题吗(我不这么认为,因为我给予了所有可能的权利,但错误仍然存​​在),它是我的代码上的东西吗(但它在我的开发机器上工作......)。 如果您需要,我可以展示更多代码!

错误:

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

这是执行上传的代码。 也许你对正则表达式的看法是正确的。 但为什么它在开发版上运行而不是在生产版上运行?

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;

}

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

为你拒绝所有暧昧 2024-08-05 22:44:41

您需要发布您的代码,但是为了在黑暗中拍摄......

在您页面上的 btnFinish_Click 方法中,您尝试使用正则表达式的位置有问题。

最有可能的是,您捕获了一组正则表达式匹配项并尝试枚举它们,而实际上根本没有匹配项。 (或者你有一个 For 循环,它遍历的项目比集合/列表实际拥有的项目还要多。)

编辑: 我 99% 确定它就在这里:

Match m = r.Match(filepath);

在你做任何其他事情之前,在这一行之后,检查是否有任何组。

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

然后,查看该组中是否有任何捕获:

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

最终您将通过执行此操作找出输入出了什么问题,但是查看代码而不是主动调试它,这是找出问题的唯一好方法。

编辑2:顺便说一句,原则上您遇到此问题的原因是因为您在尝试使用输入之前尚未真正验证输入。 我刚刚作为示例提供的代码将帮助您入门,但您应该始终清理即将出现的内容。

另外,如果您使用上传控件,并非所有浏览器都会传递文件的完整 UNC 路径(即 \server\share\file.ext) - 有些浏览器只会传递文件名本身,因此这些是一些需要检查的事情。

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.

悟红尘 2024-08-05 22:44:41

string file_ext = m.Groups[2].Captures[0].ToString();
字符串文件名 = m.Groups[1].Captures[0].ToString();

您的代码假定这些组存在。 由于某种原因(老实说,我没有正则表达式),您没有得到您认为应该始终存在于生产中的组。 我会确保这里是一个 m,有 m.Groups 和 m.Groups.Count >= 2 并且 m.Groups[] 在调用这些方法之前已捕获。

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.

春花秋月 2024-08-05 22:44:41

也许生产环境将文件放置在不同的位置。 尝试检查生产服务器放置他的文件的文件夹以及开发的位置。 当开发和上线使用不同的操作系统或不同的IIS版本时,这些问题经常发生。 存储位置的差异可能会导致您的正则表达式失败。 我不是正则表达式专业人士,所以我不知道您的正则表达式是否可能包含错误,但这是我能想到的第一件事。

另外,DEV是你自己的机器吗? 在这种情况下:您使用的是 IIS 还是 ASP.NET 开发服务器? 因为 IIS 和 Visual Studio 中集成的 ASP.NET 开发服务器在某些情况下的行为有所不同。

另外:许多开发人员认为直接“跳入”数组位置是不好的做法(我也认为假设存在正确数量的项目就跳入数组并不是很好)。 特别是在使用多维数组时,发生错误时可能会变得很棘手。 我见过许多复杂的代码在数组索引上失败,并且因为没有检查,所以很难调试它们(我指的是 5 维或 6 维数组)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文