繁体   English   中英

发布图像时“ await httpClient.PostAsync”上的对象处置实例

[英]Object Disposed Exeption on “await httpClient.PostAsync” when posting images

嗨,大家好,在Xamarin中将以下代码添加到我的MultipartFormDataContent中时,我得到一个ObjectDisposedException

JToken jobectImages = rootObject["Images"];

                foreach (var item in jobectImages)
                {
                    foreach (var internalitem in item)
                    {
                        foreach (var imageGroup in internalitem)
                        {
                            foreach (JObject image in imageGroup)
                            {
                                string sFileName = "";
                                string sFile2 = "";

                                try
                                {
                                    sFileName = image.GetValue("FileName").ToString();
                                    sFile2 = image.GetValue("FilePath").ToString();
                                }
                                catch
                                {
                                    sFileName = "";
                                    sFile2 = "";
                                }

                                if (sFile2 != "" && File.Exists(sFile2))
                                {
                                    StreamContent s = await Imagestream(sFile2);
                                    if (s != null)
                                    {
                                        multipartContent.Add(s, sFileName, sFileName);
                                    }
                                }
                            }
                        }
                    }
                }

波纹管是我的图像流方法

public async Task<StreamContent> Imagestream(String FilePath)
        {
            StreamContent streamContent2 = null;
            if (File.Exists(FilePath))
            {
                FileStream fs2 = File.OpenRead(FilePath);
                streamContent2 = new StreamContent(fs2);
                streamContent2.Headers.Add("Content-Type", "application/octet-stream");
            }
            return streamContent2;
        }

在此行获取错误

var response = await httpClient.PostAsync(GlobalVariables.url, multipartContent).ConfigureAwait(false);

我在此之前也使用上面的Imagestream方法向MultipartFormDataContent添加了另一张图像...任何帮助将非常感谢

所以原来是我的StreamContent()它在被分配给MultipartFormDataContent之前已被自动处置。要解决此问题,我使用了以下命令

 MemoryStream memoryStream = new MemoryStream();
                    streamContent.Position = 0;
                    streamContent.CopyTo(memoryStream);
                    memoryStream.Position = 0;
                    StreamContent content = new StreamContent(memoryStream);
                    content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");


            multipartContent.Add(content, "signature", "signature.png");

暂无
暂无

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

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