Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

C#
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);
        }
    }
Posted
Updated 15-Jun-15 1:28am
v2
Comments
Krunal Rohit 15-Jun-15 7:29am    
string Fp = @"~/images/" + FileName;

Try "@" before fiel path.
If you already have a physical path, it doesn't make sense to call Server.MapPath.

-KR
venkatesh (chennai) 15-Jun-15 7:45am    
again im getting error .....plz send ur mail id i will send my full source code...plz help

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.

C#
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);
 
Share this answer
 
Comments
venkatesh (chennai) 15-Jun-15 7:51am    
now error not came .....but image not loaded in the textbox ....
ZurdoDev 15-Jun-15 7:53am    
That's because you used an img tag which gets loaded in the html on the client side which means the src attribute needs to be the url to the image.
venkatesh (chennai) 15-Jun-15 7:55am    
hmmm..ok...can u sent your mail id,ill send my source code....i m a learner plz help me
ZurdoDev 15-Jun-15 7:59am    
Use an asp:Image instead of a richtextbox. https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.image.imageurl(v=vs.110).aspx
venkatesh (chennai) 15-Jun-15 8:08am    
when the below code is my project source code
http://www.codeproject.com/Questions/1000897/How-to-export-textbox-values-to-Ms-word?arn=0
Try this. You have used MapPath twice, which is not needed actually.
C#
FileUpload1.SaveAs(Server.MapPath(@"~/images/" + filename));
 
Share this answer
 
Comments
venkatesh (chennai) 15-Jun-15 8:13am    
when the below code is my project source code
and my project is .....richtexteditor text box value and images are export to Ms word or pdf format....plz help to me
http://www.codeproject.com/Questions/1000897/How-to-export-textbox-values-to-Ms-word?arn=0
C#
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);
        }
    }
 
Share this answer
 
Comments
venkatesh (chennai) 15-Jun-15 8:16am    
again i m getting error only.....
ERROR:Could not find a part of the path 'C:\~\images\see.jpg'.
venkatesh (chennai) 15-Jun-15 8:18am    
when the below code is my full project source code
and my project is .....richtexteditor text box value and images are export to Ms word or pdf format....plz help to me
http://www.codeproject.com/Questions/1000897/How-to-export-textbox-values-to-Ms-word?arn=0

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900