簡體   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