Xamarin:将图像上传到服务器

时间:2017-01-16 12:12:04

标签: c# api xamarin xamarin.forms

我需要使用api将图像上传到服务器。 现在我正在使用System.Net.Http;

byte[] lFileBytes= DependencyService.Get<IFileHelper>().ReadAllBytes(ImagePath);
ByteArrayContent lFileContent = new ByteArrayContent(lFileBytes,0,lFileBytes.Length);
lFileContent.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse("form-data");
lFileContent.Headers.ContentType=new MediaTypeHeaderValue("image/jpg");
lFileContent.Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue("name","file"));
lFileContent.Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue("filename", "9.jpg"));
lFileContent.Headers.ContentLength= lFileBytes.Length;
lContent.Add(lFileContent);

    public byte[] ReadAllBytes(string path) {
                using (var streamReader = new StreamReader(path))
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        streamReader.BaseStream.CopyTo(memoryStream);
                        return memoryStream.ToArray();
                    }
                }
            }

发送请求后发生错误Type file is invalid 我正在考虑byte[] ReadAllBytes(string path)中的问题 对于请求我可以使用Stream或byte [] 请帮忙 更新

 lRequestResponse = await lHttpClient.PostAsync("URL", lContent);

2 个答案:

答案 0 :(得分:0)

如果您仍然需要,这对我有用:

var cont = new MultipartFormDataContent();
var image = new StreamContent(img.Image.GetStream());
cont.Add(image, "\"file\"", img.FileName);
var uri = App.apiurl + $"FileUpload/" + img.FileName + "/";
using (var client = new HttpClient())
{
    var response = await client.PostAsync(uri, cont);
    if (response.StatusCode != System.Net.HttpStatusCode.OK)
    {
        // return error code
    }

}

答案 1 :(得分:0)

这对我有帮助

MultipartFormDataContent  lContent=new MultipartFormDataContent();
byte[]  lBytes = DependencyService.Get<IFileHelper>().ReadAllBytes(filename);
ByteArrayContent lFileContent= new ByteArrayContent(lBytes);
lFileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                                            {
                                                FileName = filename,
                                                Name = "imgFile"
                                            };
lFileContent.Headers.ContentType = new MediaTypeHeaderValue("image/png");
lContent.Add(lFileContent);
HttpResponseMessage lRequestResponse=await lHttpClient.PostAsync(await url, lContent);

在IOS上实施IFileHelper

namespace Client.iOS
{
    public class FileHelper : IFileHelper
    {
        private string GetLocalFilePath(string filename)
        {
            string docFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string libFolder = Path.Combine(docFolder, "..", "images", "Databases");

            if (!Directory.Exists(libFolder))
            {
                Directory.CreateDirectory(libFolder);
            }

            return Path.Combine(libFolder, filename);
        }
        public byte[] ReadAllBytes(string filename)
        {
            string path=GetLocalFilePath(filename);
            return (FileStream(path) as MemoryStream).ToArray();
        }
        private Stream FileStream(string path)
        {
            StreamReader lStreamReader = new StreamReader(path);
            MemoryStream lMemoryStream = new MemoryStream();
            lMemoryStream.Position = 0;
            lStreamReader.BaseStream.CopyTo(lMemoryStream);
            return lMemoryStream;
        }
    }
}