使用fileupload控件在数据库中上载文件

本文关键字:上载 文件 数据库 fileupload 控件 使用 | 更新日期: 2023-09-27 18:29:24

我在asp.net页面上有一个FileUpload控件和一个图像控件,如下所示:

<tr>
                <td>&nbsp;</td>
                <td align="left">
                     <asp:Image ID="profileimage" runat="server" ImageUrl="~/Images /images2.jpg" 
                     Width="150px" Height="150px" />
                </td>
                <td>&nbsp;</td>
                <td colspan="2" align="left">
                    <asp:FileUpload ID="Fu" runat="server" Width="550px" />
                </td>
            </tr>

现在如何使用代码隐藏来访问上传到fileupload控件中的文件路径,并将其存储在数据库中

使用fileupload控件在数据库中上载文件

try:

protected void btnSubmit_Click(object sender, EventArgs e) 
 {
  if (Fu.HasFile) {
  string filepath = Fu.PostedFile.FileName;
  //save the file to the server
  Fu.PostedFile.SaveAs(Server.MapPath(".''") + file);
  lblStatus.Text = "File Saved to: " + Server.MapPath(".''") + file;
 }
} 

使用此代码:

protected void btnSubmit_Click(object sender, EventArgs e) 
{
   foreach (UploadedFile upload in Fu.UploadedFiles)
                {
                    Fu.TargetFolder = "/Attachment/ClientProfile";
                    path = Server.MapPath(Fu.TargetFolder) + "''" + upload.GetName();
                    upload.SaveAs(path);
                }
//Here give ImageName=path to save in database.
}