嗨我想上传带有文字信息的图片json和wcf图片包含标题和描述任何帮助请
功能 实现以下接口,我正在使用json和wcf
public wsResultImage InsertPhoto(Stream JsonDataStream)
{
wsResultImage results = new wsResultImage();
StreamReader reader = new StreamReader(JsonDataStream);
string JSONdata = reader.ReadToEnd();
// bool logged;
// ..then convert the string into a single "wsOrder" record.
JavaScriptSerializer jss = new JavaScriptSerializer();
wsImage photoo = jss.Deserialize<wsImage>(JSONdata);
// byte[] bytes= (byte[]) (photoo.GetImage).ToArray();
if (photoo == null)
{
results.id = photoo.GetId;
results.titlee = photoo.GetTitle;
results.imag = photoo.GetImage;
results.descriptionn = photoo.GetDescrption;
results.message = "all fields are required";
return results;
}
FamissimaDataContext dc = new FamissimaDataContext();
/* imagees photo = (from p
in dc.imagees
select p).FirstOrDefault(); */
// byte[] bytes= (byte[]) (photoo.GetImage).ToArray();
// string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
// where (photoo.GetId = p.id)
byte[] imageBytes = Convert.FromBase64String(photoo.GetImage);
imagees newPhoto = new imagees()
{
id = photoo.GetId,
// imagee = System.Text.Encoding.ASCII.GetBytes(photoo.GetImage),
// imagee = imageBytes,
title = photoo.GetTitle,
description = photoo.GetDescrption,
};
dc.imagees.InsertOnSubmit(newPhoto);
dc.SubmitChanges();
// Console.WriteLine("Hello C# World :-) ");
results.message = "Success";
// result.Exception = "";
return results;
}
界面:
[OperationContract]
[WebInvoke(Method= "Post", ResponseFormat = WebMessageFormat.Json, UriTemplate = "uploadimage")]
wsResultImage InsertPhoto(Stream JsonDataStream);
答案 0 :(得分:0)
以下示例可以帮助您:
注意我截断了编码的字符串,因为它太长了。
void Main()
{
string json = "{Title: 'Image title', Description: 'Description', Base64Data : 'iVBORw0KGgoAAAANSUhEUgAAAA0AAAAQCAYAAADNo/U5AAAACXBIWXMAAA......'}";
JavaScriptSerializer js = new JavaScriptSerializer();
var picture = js.Deserialize<Picture>(json);
// Now deserialize the Base64 encoded picture.
picture.Data = Convert.FromBase64String(picture.Base64Data);
File.WriteAllBytes(@"C:\test.png", picture.Data);
}
class Picture
{
public string Title {get; set;}
public string Description {get; set;}
public byte[] Data {get; set;}
public string Base64Data {get; set;}
}