上传后不存储图像

时间:2014-02-19 09:36:45

标签: c# asp.net

上传尝试后,图像未存储到Images文件夹中。我的代码出了什么问题?

这是我的代码:

protected void btnupload_Click(object sender, EventArgs e)
{   
   if (fileupload1.HasFile)
   {
      string fileName = fileupload1.FileName.ToString();
      string uploadFolderPath = "~/Image/";

      string filePath = HttpContext.Current.Server.MapPath(uploadFolderPath);

      fileupload1.SaveAs(filePath + "\\" + fileName);

      img1.ImageUrl = "~/Image/" + "/" + fileupload1.FileName.ToString();
      lblimg_name.Text=  fileupload1.FileName.ToString();
   }
}

3 个答案:

答案 0 :(得分:1)

如果您使用<asp:FileUpload>,请尝试以下操作:

或详细描述

string strFileName = "fileName";
string strFileType = System.IO.Path.GetExtension(fileupload1.FileName).ToString().ToLower();
fileupload1.SaveAs(Server.MapPath("folderpath" + strFileName + strFileType));

答案 1 :(得分:0)

更改

img1.ImageUrl = "~/Image/" + "/" + fileupload1.FileName.ToString();

img1.ImageUrl = "~/Image/" +  fileupload1.FileName;

您的路径中还有其他"/"

答案 2 :(得分:0)

试试这段代码..

protected void btnupload_Click(object sender, EventArgs e)
{
   if (fileupload1.HasFile)
    {
        string fileName = Path.GetFileName(fileupload1.PostedFile.FileName);
        fileupload1.PostedFile.SaveAs(Server.MapPath("~/Image/") + fileName);

    }
}