记录一个简单webapi 上传图片
图片转base64字符串,到接口后,再转回保存.代码里面是转成byte[]之后转的,也可以用base64字符串直接转图片.
只想记录一下简单的流程。
1,服务端
保存图片业务代码:
public class UpLoadFile
{
public string UpLoad(string fileName,string filePath,byte[] fileData)
{
Bitmap map =BitmapFromBytes(fileData);
filePath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/") +"images\\" + fileName;
map.Save(filePath, ImageFormat.Jpeg);
return filePath;
}
{
public string UpLoad(string fileName,string filePath,byte[] fileData)
{
Bitmap map =BitmapFromBytes(fileData);
filePath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/") +"images\\" + fileName;
map.Save(filePath, ImageFormat.Jpeg);
return filePath;
}
/// <summary>
/// byte[]数组转换为Bitmap
/// </summary>
/// <param name="bytes">byte[]数组</param>
/// <returns></returns>
public static Bitmap BitmapFromBytes(byte[] bytes)
{
Bitmap bitmap = null;
try
{
using (MemoryStream stream = new MemoryStream(bytes))
{
bitmap = new Bitmap((Image)new Bitmap(stream));
}
}
catch { }
/// byte[]数组转换为Bitmap
/// </summary>
/// <param name="bytes">byte[]数组</param>
/// <returns></returns>
public static Bitmap BitmapFromBytes(byte[] bytes)
{
Bitmap bitmap = null;
try
{
using (MemoryStream stream = new MemoryStream(bytes))
{
bitmap = new Bitmap((Image)new Bitmap(stream));
}
}
catch { }
return bitmap;
}
}
}
}
控制器代码:
在这里使用dynamic的时候犯了一个错,特别指出,使用过这个Convert.FromBase64String(fileInfo.fileData),这是不对的,fileInfo.fileData并不是string,写成这样就是自己思想固化的直接体现。。。
public class UpLoadController : ApiController
{
[HttpPost]
public string UpLoadFile(dynamic fileInfo)
{
string fileName = fileInfo.fileName;
string filePath = string.Empty;
string fileData1 = fileInfo.fileData;
byte[] fileData = Convert.FromBase64String(fileData1);
BLL.UpLoadFile upLoad = new BLL.UpLoadFile();
{
[HttpPost]
public string UpLoadFile(dynamic fileInfo)
{
string fileName = fileInfo.fileName;
string filePath = string.Empty;
string fileData1 = fileInfo.fileData;
byte[] fileData = Convert.FromBase64String(fileData1);
BLL.UpLoadFile upLoad = new BLL.UpLoadFile();
return upLoad.UpLoad(fileName, filePath, fileData);
}
[HttpPost]
public string Test()
{
return "123";
}
}
}
[HttpPost]
public string Test()
{
return "123";
}
}
2,客户端代码,winfrom
调用接口
private async void button3_Click(object sender, EventArgs e)
{
//创建一个处理序列化的DataContractJsonSerializer
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(FileInfo));
MemoryStream ms = new MemoryStream();
FileInfo fileInfo = new FileInfo()
{
fileName = "123.jpeg",
fileData =Convert.ToBase64String( GetImage())
};
//将资料写入MemoryStream
serializer.WriteObject(ms, fileInfo);
//一定要在这设定Position
ms.Position = 0;
HttpContent content = new StreamContent(ms);//将MemoryStream转成HttpContent
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
HttpClient client = new HttpClient();
HttpResponseMessage response = client.PostAsync("http://localhost:803/api/UpLoad/UpLoadFile" , content).Result;
if (response.IsSuccessStatusCode)
{
string ret = response.Content.ReadAsStringAsync().Result;
string json = JsonConvert.DeserializeObject(ret).ToString();
MessageBox.Show("成功 :"+json);
}
}
{
//创建一个处理序列化的DataContractJsonSerializer
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(FileInfo));
MemoryStream ms = new MemoryStream();
FileInfo fileInfo = new FileInfo()
{
fileName = "123.jpeg",
fileData =Convert.ToBase64String( GetImage())
};
//将资料写入MemoryStream
serializer.WriteObject(ms, fileInfo);
//一定要在这设定Position
ms.Position = 0;
HttpContent content = new StreamContent(ms);//将MemoryStream转成HttpContent
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
HttpClient client = new HttpClient();
HttpResponseMessage response = client.PostAsync("http://localhost:803/api/UpLoad/UpLoadFile" , content).Result;
if (response.IsSuccessStatusCode)
{
string ret = response.Content.ReadAsStringAsync().Result;
string json = JsonConvert.DeserializeObject(ret).ToString();
MessageBox.Show("成功 :"+json);
}
}
实体类
[DataContract]
public class FileInfo
{
[DataMember]
public string fileName
{
get;
set;
}
[DataMember]
public string fileData
{
get;
set;
}
}
public class FileInfo
{
[DataMember]
public string fileName
{
get;
set;
}
[DataMember]
public string fileData
{
get;
set;
}
}