微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ReadTimeout = 'target.ReadTimeout' 在 ASP.NET MVC 中引发了类型为“System.InvalidOperationException”的异常

如何解决ReadTimeout = 'target.ReadTimeout' 在 ASP.NET MVC 中引发了类型为“System.InvalidOperationException”的异常

我正在尝试使用 Web API 在 ASP.NET MVC 中上传图像,但出现异常。此异常最初是在此调用堆栈中引发的:

System.IO.Stream.ReadTimeout.get()
Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(object)

下面是我添加新产品的代码,其中还有其他字段,如名称、价格、类别,这些字段工作正常,但在图像的情况下,我遇到异常:

public ActionResult Save(ProductInfo productInfo)
{
    if(!ModelState.IsValid)
    {
        return View("New");
    }

    if(productInfo.product_Id == 0)
    {
        // context.ProductInfos.Add(productInfo);
        using(var client = new HttpClient())
        {
            using(var content = new MultipartFormDataContent())
            {
                MemoryStream target = new MemoryStream();
                productInfo.ImageFile.InputStream.copyTo(target);
                byte[] Bytes = target.ToArray();

                productInfo.ImageFile.InputStream.Read(Bytes,Bytes.Length);
                var fileContent = new ByteArrayContent(Bytes);
                fileContent.Headers.Contentdisposition = new System.Net.Http.Headers.ContentdispositionHeaderValue("attachment") { FileName = productInfo.ImageFile.FileName };
                content.Add(fileContent);
                //  var Result =  client.PostAsync("http://localhost:52071/api/",content).Result;
            }
        }
           
        HttpResponseMessage response = GlobalVariables.Webapiclient.PostAsJsonAsync("products",productInfo).Result;
        TempData["SuccessMesssage"] = "Product saved successfully";
    }
    else
    {
        HttpResponseMessage response = GlobalVariables.Webapiclient.PutAsJsonAsync("products/"+productInfo.product_Id,productInfo).Result;
        TempData["SuccessMesssage"] = "Product updated successfully";
    }
       
    // context.SaveChanges();
    return RedirectToAction("Index","Product");
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。