Windows 8 xaml应用程序问题与multipartformdatcontent不上传图像

本文关键字:图像 multipartformdatcontent xaml 应用程序 问题 Windows | 更新日期: 2023-09-27 18:06:07

我正在尝试从用户本地文件夹上传图像文件

我写了这个方法来获取文件流和url,然后上传它。问题是,虽然我没有得到错误,并且服务器上的图像获得了自己的id等,但它看起来是空的。

我假设我上传的流有问题,当我在那里设置断点时,我看到它里面的字节,所以它不是空的

public static async Task<string> UploadImage(string url, StorageFile file)
    {
        HttpClient httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Add("Authorization", authKey);
        try
        {   
            IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);
            MultipartFormDataContent form = new MultipartFormDataContent();
            form.Add(new StringContent("name"), "myphoto");
            var content = readStream.AsStream();
            form.Add(new StreamContent(content));
            HttpResponseMessage response = await httpClient.PostAsync(url, form);
            string res = response.Content.ToString();
            return res;
        }
        catch (HttpRequestException hre)
        {
            return string.Empty;
        }
        catch (Exception ex)
        {
            return string.Empty;   
        }            
    }

Windows 8 xaml应用程序问题与multipartformdatcontent不上传图像

这毕竟是一个web服务问题