Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将图像上载到文件夹并显示_C#_Asp.net - Fatal编程技术网

C# 如何将图像上载到文件夹并显示

C# 如何将图像上载到文件夹并显示,c#,asp.net,C#,Asp.net,我试着通过按一个提交按钮将图片上传到整个表单的文件夹(使用FileUpload)。我设法将图像上载到单独的文件夹,但无法显示它。 多谢各位 String fname; FileUpload tempFU = new FileUpload(); string path = Server.MapPath(".") + "\\images\\" + ulProj.groupCode; if (!Directory.Exists(path)) {

我试着通过按一个提交按钮将图片上传到整个表单的文件夹(使用FileUpload)。我设法将图像上载到单独的文件夹,但无法显示它。 多谢各位

    String fname;
    FileUpload tempFU = new FileUpload();
    string path = Server.MapPath(".") + "\\images\\" + ulProj.groupCode;
    if (!Directory.Exists(path))
    {
        Directory.CreateDirectory(path);
    }
    try
    {
        tempFU = (FileUpload)customerUC.FindControl("CustomerLogoUrlFU");
        Directory.CreateDirectory(path);
        fname = path + "\\" + tempFU.FileName;
        tempFU.SaveAs(fname);
        tempCus.logoUrl = fname;
    }
    catch
    {
        //return;
    }

要记住的要点:

  • 您应该使用
    tilde
    ~运算符来表示当前项目
    根文件夹

  • 使用
    System.IO.Path.Combine()
    组合您的
    路径
    文件名
    以获得有效的完整路径

  • 您正在为给定的
    路径创建
    目录
    两次。因此,删除第二次创建
    目录的后面部分

  • 正如上面
    注释中所述,由于
    catch
    块没有任何代码, 删除
    try catch

  • 完整解决方案:

        String fname;
        FileUpload tempFU = new FileUpload();
        string path = Server.MapPath(@"~\images\" + ulProj.groupCode);
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
    
        tempFU = (FileUpload)customerUC.FindControl("CustomerLogoUrlFU");        
        fname = System.IO.Path.Combine(path,tempFU.FileName);
        tempFU.SaveAs(fname);
        tempCus.logoUrl = fname;
    

    您应该只将图像的(有效)路径指定给图像标记的src属性。你的问题是什么?我强烈建议你去掉那个try/catch块。我做了,但它仍然没有显示出来it@user3025441:tempCus是图像控件吗?