简体   繁体   English

Xamarin.forms 图像休息 api

[英]Xamarin.forms image to rest api

In Xamarin.Forms I am sending my Image to rest Api, while in the Add the image is not being converted as needed, getting and error cannot convert system.net.http.streamcontent to byte[]在 Xamarin.Forms 中,我将图像发送到 rest Api,而在 Add 中,图像未根据需要进行转换,获取和错误无法将 system.net.http.streamcontent 转换为 byte[]

HttpClient client = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();
form.Add(new ByteArrayContent(new StreamContent(new MemoryStream(image1)), "bilddatei", "upload.jpg"));

In the code mentioned image1 is an image taken from camera using Xamarin.Forms, please help me resolve this, or let me know of alternate options to send image to Rest web service.在代码中提到的 image1 是使用 Xamarin.Forms 从相机拍摄的图像,请帮助我解决这个问题,或者让我知道将图像发送到 Rest Web 服务的替代选项。

Looks like you need to pass a byte[] instead of a StreamContent .看起来您需要传递一个byte[]而不是StreamContent Try this:试试这个:

var client = new HttpClient();
var form = new MultipartFormDataContent();
form.Add(new ByteArrayContent(new MemoryStream(image1).ToArray()), "bilddatei", "upload.jpg");

Update : If you're saving the picture on device's storage, you can send it like:更新:如果您将图片保存在设备的存储中,您可以像这样发送:

var client = new HttpClient();
var form = new MultipartFormDataContent();
var path = "path/to/file";
form.Add(new ByteArrayContent(File.ReadAllBytes(path)), "bilddatei", "upload.jpg");

I understood the doubt, but I believe there is a better way to do this.我理解这个疑问,但我相信有更好的方法来做到这一点。 Using Firebase storage and writing to your database, only the image URL saved in Firebase.使用 Firebase 存储并写入您的数据库,仅保存在 Firebase 中的图像 URL。

Like this:像这样:

> >

using (var ms = new MemoryStream(file.ByteFile))
{
    ms.Seek(0, SeekOrigin.Begin);

    var task = new FirebaseStorage(
        "your.adress.firebase.com",
         new FirebaseStorageOptions()
         {
             HttpClientTimeout = new TimeSpan(0, 30, 0)
         })
         .Child("Files")
         .Child($"{file.id}.mp4")
         .PutAsync(ms);

         file.LinkVideo = await task;
         Entity entity = new Entity()
         {
             Name = file.Name,
             LinkVideo = file.LinkVideo,
         };
         //Your API
         return await PostEntity(contest);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM