简体   繁体   中英

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. I try to convert image from FromStream and than post it to server via PostSync method. 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

    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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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