【问题标题】:fileupload.PostedFile.SaveAs not save image in folder in asp.netfileupload.PostedFile.SaveAs 不将图像保存在 asp.net 的文件夹中
【发布时间】:2018-05-26 20:18:49
【问题描述】:
string fileName = Path.GetFileName(fup.PostedFile.FileName);
fileName = Guid.NewGuid() + fileName;

if (Path.GetExtension(fup.PostedFile.FileName) == ".jpg" || Path.GetExtension(fup.PostedFile.FileName) == ".jpeg" || Path.GetExtension(fup.PostedFile.FileName) == ".bmp" || Path.GetExtension(fup.PostedFile.FileName) == ".png")
{
    string s = Server.MapPath("~/Images/" + fileName);
    fup.PostedFile.SaveAs(s);
    how = "file";

    SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString);
    SqlCommand cmd = new SqlCommand(@"update product set sub_cat = '" + ddlcategory.SelectedValue.Trim() + "',name='" + Pnam.Text.Trim() + "',pic='" + fileName + "',price=" + price.Text.Trim() + ",description='" + desc.Text.Trim() + "',unit='" + ddlUnit.SelectedValue.Trim() + "',catgeory='" + ddlcat.SelectedValue.Trim() + "'  where product_id=" + pid.Text.Trim(), cn);
    cn.Open();
    cmd.ExecuteNonQuery();
    cn.Close();
    gvproduct.EditIndex = -1;
    fillgrid();

    show = "Update";
}

这是我将图像文件保存在图像(文件夹)中的代码。执行此查询后,没有图像保存在文件夹中。但是这些图片显示在GridView, 哪个代码是这样的:

<asp:TemplateField HeaderText="Image">
  <ItemTemplate>
      <asp:Image ID="imgPd" runat="server" Height="60px"
          ImageUrl='<%#"~/Images/"+Eval("pic").ToString() %>' Width="60px" />
  </ItemTemplate>
  <EditItemTemplate>
      <asp:FileUpload ID="FileUploadGV" runat="server" Width="50px" />

【问题讨论】:

    标签: c# asp.net sql-server gridview save-as


    【解决方案1】:

    我在下面做了一个将文件保存到文件夹的方法。

    您可以将文件夹名(例如Images)和文件名(例如FileUploadGV.PostedFile)传递给方法。

    private void UploadFile(string FolderName, HttpPostedFile file)
    {
        // make folder path
        string FolderPath = "~\\" + FolderName;
    
        // create folder directory info
        DirectoryInfo FolderDir = new DirectoryInfo(Server.MapPath(FolderPath));
    
        // check if folder directory not exist
        if (!FolderDir.Exists)
        {
            // create directory
            FolderDir.Create();
        }
    
        // define file path
        string FilePath = Path.Combine(Server.MapPath(FolderPath), file.FileName);
    
        // check if file not exist
        if (!File.Exists(FilePath))
        {
            // save file into folder directory
            file.SaveAs(FilePath);
        }
    }
    

    额外:要从文件夹中删除文件,请使用此方法:

    private void DeleteFile(string FileName)
    {
        // make file path
        string path = Server.MapPath(FileName);
    
        // check if file exist
        if (File.Exists(path))
        {
            // delete file from folder
            File.Delete(path);
        }
    }
    

    【讨论】:

    • 图像存储在文件夹中,但这些不包含在项目中。如何将这些图像包含在项目中。
    • 转到解决方案资源管理器 --> 单击显示所有文件图标,然后右键单击文件 --> 从弹出窗口中选择包含在项目中。
    • 我在服务器上上传这个项目时出现问题。图像没有保存在文件夹中
    • 应用程序池文件夹访问问题
    • 如果您有新问题,请提出新问题:stackoverflow.com/questions/ask
    【解决方案2】:

    在服务器上,Images(我保存图像的地方)文件夹没有应用程序池权限。授予读写权限后。此问题已解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-21
      • 2016-01-20
      • 1970-01-01
      • 2021-08-12
      • 2014-12-11
      • 1970-01-01
      相关资源
      最近更新 更多