简体   繁体   English

使用HttpClient.PostySync时ASP MVC图像上传错误

[英]ASP MVC Image upload error when using HttpClient.PostySync

I have asp mvc web page to upload image, I need to validate the image width and height. 我有asp mvc网页上传图片,我需要验证图片的宽度和高度。 I try to convert image from FromStream and than post it to server via PostSync method. 我尝试从FromStream转换图像,然后通过PostSync方法将其发布到服务器。 I do not get any error but image is not posting to the server. 我没有得到任何错误,但图像没有发布到服务器。 If I bypass the FromStream method, than I do not see any error 如果我绕过FromStream方法,我没有看到任何错误

    public virtual ActionResult SaveFileConfigure(ConfigurationDto configuration, HttpPostedFileBase filePost)
    {
         System.IO.Stream stream = filePost.InputStream;
         System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
          //check image width here
          WebApiClient.UploadFile(this.FileService, stream, configuration.FileName);
    }

Here is web api upload code 这是web api上传代码

public static void UploadFile(string serviceUrl, Stream file, string fileName)
        {
            using (var client = new HttpClient())
            {
                using (var content = new MultipartFormDataContent())
                {
                    var fileContent = new StreamContent(file);
                    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {
                        FileName = fileName
                    };
                    content.Add(fileContent);

                    var result = client.PostAsync(string.Format("{0}/upload", serviceUrl.TrimEnd('/')), content).Result;
                }
            }
}

I am able to fix the issue by using FromFile method. 我可以使用FromFile方法解决问题。

System.Drawing.Image image = System.Drawing.Image.FromFile(filePost.FileName); System.Drawing.Image image = System.Drawing.Image.FromFile(filePost.FileName);

Looks like after I use FromStream method, the stream is getting closed and not able to post the file. 看起来在我使用FromStream方法后,流被关闭而无法发布文件。

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

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