ASP.net c# FileUpload & On PageLoad

本文关键字:On PageLoad amp net FileUpload ASP | 更新日期: 2023-09-27 18:36:03

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

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

我正在单击保存按钮时使用文件上传。

 <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 

任何帮助将不胜感激,希望我清楚地说明了我的问题。

ASP.net c# FileUpload & On PageLoad

试试这个,我希望这可以帮助你:

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";
}