为什么图像不输入变量?

时间:2019-12-25 13:42:35

标签: c# asp.net filenames

aspx:

<asp:FileUpload runat="server" ID="PicUpload" CssClass="btn btn-sm xor"/>

aspx.cs:

PicUpload.SaveAs(Path.Combine("C:\\Users\\KeepKids\\KeepKids\\pics\\", PicUpload.FileName));

为什么要PicUpload.FileName =""

在一处,我执行相同的操作,然后得到图片。然后我得到“”

2 个答案:

答案 0 :(得分:1)

我从下面的链接中找到:

https://www.aspforums.net/Threads/136183/ASPNet-FileUpload-control-Issue-FileName-is-blank-always-blank/

您可以尝试一下吗?

**PicUpload.PostedFile.FileName**

如下所示:

protected void btnsubmit_Click(object sender, EventArgs e)
        {
 string filename = "";
            if (uploadphoto.PostedFile != null)
            {
                filename = Path.GetFileName(uploadphoto.PostedFile.FileName);
                if (filename != "")
                {
                    uploadphoto.SaveAs(Server.MapPath("images/" + filename));
                    string path = "images/" + filename;

                }
            }
}

答案 1 :(得分:1)

<asp:FileUpload ID="FileUploadControl" runat="server" />
&nbsp;
<asp:Button ID="btnUpload" runat="server" Text="Upload File" 
    onclick="UploadButton_Click" />
<br />
<asp:Label ID="lblMessage" Font-Bold="true" runat="server">
</asp:Label>

下面是单击事件,该事件从FileUploadControl中拾取文件名

protected void UploadButton_Click(object sender, EventArgs e)
{
    if(FileUploadControl.HasFile)
    {
        try
        {
            string filename = Path.GetFileName(FileUploadControl.FileName);
            FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
            lblMessage.Text = "Upload status: File uploaded!";
        }
        catch(Exception ex)
        {
            lblMessage.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}