【问题标题】:ASP.net c# FileUpload & On PageLoadASP.net c# FileUpload & On PageLoad
【发布时间】:2015-01-02 20:42:10
【问题描述】:

我正在尝试编辑个人资料页面..

在页面加载时,用户从数据库中读取数据并显示。

我在点击保存按钮时使用文件上传..

 <asp:Label ID="Label17" runat="server" Text="Logo"></asp:Label>
    <div id="photo" >
    <asp:Image ID="Image1" runat="server" Height="155px" Width="173px" />
    <asp:FileUpload ID="FileUpload1" runat="server" Width="220px " />
    </div>

C#

        System.IO.FileInfo file = new System.IO.FileInfo(FileUpload1.PostedFile.FileName);
        string fname = file.Name.Remove((file.Name.Length - file.Extension.Length));
        fname = fname + Guid.NewGuid().ToString("N") + file.Extension;
        string photo = "";
        photo = fname;
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(Server.MapPath("~/images/dp/" + fname));
            Image1.ImageUrl = "~/images/dp/" + fname;
        }
        else
        {
            //  saved.InnerText = "Please Select Employee Photo";
        }

页面加载功能:

Image1.ImageUrl = "~/images/dp/" + myReader3["s_dp"].ToString();

嗯,上传功能正常,保存到数据库中,图片加载到下一页加载正常...

问题是,如果我在不选择新文件的情况下单击配置文件保存按钮,则会出现错误。如果选择了文件,它可以工作并保存在数据库中。

但是例如,如果我要编辑名称而不对文件上传区域做任何事情,然后单击保存按钮...弹出错误...

The path is not of a legal form.

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.ArgumentException: The path is not of a legal form.

Source Error: 


Line 111:        string sid;
Line 112:        sid = Request.QueryString["SID"].ToString();
Line 113:        System.IO.FileInfo file = new System.IO.FileInfo(FileUpload1.PostedFile.FileName);
Line 114:        string fname = file.Name.Remove((file.Name.Length - file.Extension.Length));
Line 115:        fname = fname + Guid.NewGuid().ToString("N") + file.Extension;

Source File: c:\Users\User\Documents\Visual Studio 2010\WebSites\HireMe\admin\editseeker.aspx.cs    Line: 113 

任何形式的帮助都将不胜感激,希望我能清楚地说明我的问题..

【问题讨论】:

  • 那么 page_load IsPostback 或 !IsPostback 呢?请发送完整的 page_load 代码。你使用 try-catch 了吗?
  • No try catch in pageload... 仅当 (!Page.IsPostBack)
  • 能否贴出page_load()和editprofile_save按钮的完整代码块?

标签: c# asp.net file-upload


【解决方案1】:

试试这个,希望对你有帮助:

if (FileUpload1.HasFile)
{
System.IO.FileInfo file = new System.IO.FileInfo(FileUpload1.PostedFile.FileName);
string fname = file.Name.Remove((file.Name.Length - file.Extension.Length));
fname = fname + Guid.NewGuid().ToString("N") + file.Extension;
string photo = "";
photo = fname;

FileUpload1.SaveAs(Server.MapPath("~/images/dp/" + fname));
Image1.ImageUrl = "~/images/dp/" + fname;
}
else
{
    //  saved.InnerText = "Please Select Employee Photo";
}

【讨论】:

  • 你在“else”中写了什么???如果您使用有关该文件的任何内容,也必须在“else”中将其清除
  • @mohsensolhnia :那不应该是评论而不是回答吗??
  • 感谢您的帮助,但是当我将整个上传代码封装在其中时,如果 (FileUpload1 != null) 所有错误都消失了,并且它按照我想要的方式工作:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
  • 2010-11-02
  • 1970-01-01
相关资源
最近更新 更多