将图像保存在网站上的文件夹中,无需上传文件 [英] save image in a folder on your website without fileupload

查看:59
本文介绍了将图像保存在网站上的文件夹中,无需上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的网站托管在一台服务器上,我有一个名为images的文件夹。我正在接收一个base64字符串,并将其转换为图像并将其保存在我的本地目录中,并且工作正常。



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

//将Base64字符串转换为byte []
byte [] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes,0,imageBytes.Length);

//将byte []转换为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);
}





但是当我使用

string filePath =http:// mywebsite。 COM /图像/ pic1.jpg; image.Save(filePath,ImageFormat.Jpeg);

它给我System.ArgumentException:不支持URI格式,我得到的是image.save只获取本地文件夹。所以我的问题是如何将我的图像保存在我的网站文件夹中。我找到了一些解决方案,但都使用了fileUpload,我无法使用它,因为我从android接收base64图像字符串并使用此web服务来保存图像。

解决方案

你可以保存到URL - 这就是http:// ...:它是一个URL,而不是一个路径。

尝试使用:

< pre lang =c#> string filePath = Server.MapPath( 〜/图片/ pic1.jpg);


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.

解决方案

You can;t save to URL's - which is what "http://..." is: it's a URL, not a path.
Try using:

string filePath = Server.MapPath("~/images/pic1.jpg");


这篇关于将图像保存在网站上的文件夹中,无需上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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