繁体   English   中英

ASP.net c#FileUpload和页面加载

[英]ASP.net c# FileUpload & On PageLoad

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

在页面加载的位置,用户从数据库读取数据并显示它。

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

 <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 

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

试试这个,希望对您有所帮助:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM