記錄一個簡單webapi 上傳圖片
阿新 • • 發佈:2019-05-08
name string ada bsp bitmap div 自己 成功 member
{
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 { } 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(); return upLoad.UpLoad(fileName, filePath, fileData);
}
[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);
}
} 實體類 [DataContract]
public class FileInfo
{
[DataMember]
public string fileName
{
get;
set;
}
[DataMember]
public string fileData
{
get;
set;
}
}
圖片轉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;
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 { } 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(); return upLoad.UpLoad(fileName, filePath, fileData);
}
[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);
}
} 實體類 [DataContract]
public class FileInfo
{
[DataMember]
public string fileName
{
get;
set;
}
[DataMember]
public string fileData
{
get;
set;
}
}
記錄一個簡單webapi 上傳圖片