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

当我提交一个文件通过一个形式的使用 <asp:FileUpload>, 它的工作完全可以在我的开发机。

当我试试同样的事情在服务器上,它给了我错误的下面。误差不帮助我,因为我甚至没有这种功能在我的代码 (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

这里的代码没有上传。也许你是对的与regex.但为什么是它的工作在开发和不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;

}
有帮助吗?

解决方案

你会需要你的代码,但对于一个在黑暗中拍摄...

在你btnFinish_Click方法在你的网页,有些东西错了你试图使用一个经常的表达。

最有可能你已经抓获了一群体的RegEx相匹配,并试图通过列举他们,当时真的没有任何。(或者你有一个循环将通过更多的项目比集合/名单实际。)

编辑: 我99%确定它的权利之后在这里:

Match m = r.Match(filepath);

之前你做什么,经过这一路线,检查来看看是否有任何群体。

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

然后看看是否有任何捕捉在这一组:

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

最终你会发现什么是错误输入通过这样做,但是看码和没有积极调试这个,这是唯一的良好方式找出来。

编辑2: 通过这种方式,理由在原则上,你有这个问题是因为你还没有真正验证的输入之前试图使用它。代码,我只是给了作为一个样会让你开始的,但是你应该总是清理了什么来给你。

此外,如果您使用的上载控制,并不是所有的浏览器将通过在一个充满UNC路的文件(即\服务\分享\文件。ext)-有些人会只是通过在文件名称本身,所以这些都是有些事情要检查。

其他提示

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

你的代码假定的团体存在。由于某些原因(老实说我没有regex-fu)你得不到的组认为应该始终存在于生产。我确定这里是一个m,m。团体和m。小组。数>=2和m。组[]有捕获之前,呼吁那些方法。

也许生产环境的地方的文件,在一个不同的位置。尝试检查什么的文件夹的生产服务器的地方,他的文件和那里的发展。这些问题经常发生在发展和生活使用的是不同的操作系统或不同的IIS版本。可能是差别的存储位置会导致你的reg-ex到失败。我不是一个reg-ex亲,所以我不知道是否你regex可能会包含错误的,但这是第一件事我能想到的。

此外,为开发自己的机器?在这种情况下:你使用IIS或者是你使用ASP.NET 发展服务器?因为IIS和ASP.NET 发展服务综合在Visual Studio的行为不同,在某些情况下。

也:直接"进入"一系列的位置被认为是不良做法的许多开发人员(我也觉得不是很好只是跳进入阵列假设正确数量的项目都有).特别是在使用多层面阵列可得到棘手的错误时有发生。我已经看到许多复杂的代码不在阵列的索引和因为没有检查这是很难调试(我说的是5个或6个层面阵列).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top