如何解决C:user / ...是物理路径,但预计会有虚拟路径。 [英] how to solve C:user/...is a physical path, but a virtual path was expected.

查看:292
本文介绍了如何解决C:user / ...是物理路径,但预计会有虚拟路径。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决这类错误请帮助... PLZ ...

C:user / ...是物理路径,但预计会有虚拟路径。低于代码



how to solve this type of error kindly help...plz...
C:user/...is a physical path, but a virtual path was expected. below the code

protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string FileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
            string Fp = "~/images/" + FileName; // hear only getting  error
            string FilePath = (Server.MapPath(Fp)); // hear only getting  error
            FileUpload1.SaveAs(Server.MapPath(FilePath)); // hear only getting  error
            RichTextBox.Text += string.Format("<img src = '{0}' alt = '{1}' />", FilePath, FileName);
        }
    }

推荐答案

正如KR在评论中提到的那样,Server.MapPath需要一个虚拟路径。所以你不会一遍又一遍地在同一个字符串上调用它。



As KR mentioned in comments, Server.MapPath expects a virtual path. So you would not keep calling it over and over on the same string.

string FileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
string Fp = "~/images/" + FileName; // ~ means you'll get a virtual path starting at the root of the application.  This is good.
string FilePath = Server.MapPath(Fp);  // this will give you something like "c:\inetpub\wwwroot\website1\images\filename
FileUpload1.SaveAs(FilePath);


试试这个。您使用过 MapPath 两次,实际上并不需要。

Try this. You have used MapPath twice, which is not needed actually.
FileUpload1.SaveAs(Server.MapPath(@"~/images/" + filename));


protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string FileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
            string Fp = @"~/images/" + FileName;
            string FilePath = Server.MapPath(Fp); 
            FileUpload1.SaveAs(FilePath);
            RichTextBox.Text += string.Format("<img src = '{0}' alt = '{1}' />", Fp, FileName);
        }
    }


这篇关于如何解决C:user / ...是物理路径,但预计会有虚拟路径。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆