Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have my website hosted on a server and i have a folder their named images.I am recieving a base64 string and convert it into an image and saving it in my local directory and it works perfectly.

[WebMethod]
public void UploadPics(String imageString)
{
    //HttpRequest Request = new HttpRequest();
    //HttpPostedFile filePosted = new HttpPostedFile();
    string base64String = imageString;

    // Convert Base64 String to byte[]
    byte[] imageBytes = Convert.FromBase64String(base64String);
    MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

    // Convert byte[] to Image
    ms.Write(imageBytes, 0, imageBytes.Length);
    System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
    //string newFile = Guid.NewGuid().ToString() + fileExtensionApplication;
    string filePath = "C:/Users/MUWebServices/App_Code/images/pic1.jpg";
    image.Save(filePath, ImageFormat.Jpeg);       
}


But when i use
string filePath = "http://mywebsite.com/images/pic1.jpg"; image.Save(filePath, ImageFormat.Jpeg);
it gives me "System.ArgumentException: URI formats are not supported" and i get it that image.save only gets local folders . So my question is how do i save my image on my website folder. I found some solutions but all were using fileUpload and i can't use that because i am receiving base64 image string from android and using this webservice to save images.
Posted

1 solution

You can;t save to URL's - which is what "http://..." is: it's a URL, not a path.
Try using:
C#
string filePath = Server.MapPath("~/images/pic1.jpg");
 
Share this answer
 
Comments
OriginalGriff 6-Jun-15 9:50am    
When you use Server.MapPath, the "~" acts as "root directory of my website" and is translated into the appropriate hard disk path prefix.
So "~\images\pic1.jpg" would be translated to something like "D:\Websites\decisivereason\images\pic1.jpg"
In development it refers to the local HDD and your development source folder, in production it refers to the production environment.

You can't save directly onto a webserver other than the one that your web app is running on - or at least you damn well shouldn't be able to or anyone can as well. And that's a bad idea - you would have no idea what anyone on the internet was storing in "your name" or what changes they would make to your files!

If you need to save on a remote server, then use FTP and a good strong password!
decisive reason 6-Jun-15 9:50am    
dude , thank u so so much , that solved my problem :D
u have no idea i actually wasted 1.5 days solving this problem . anyways thanks a ton :)
OriginalGriff 6-Jun-15 10:00am    
You're welcome!

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