[Solved] IIS Error: 400 bad request while uploading file in ASP.Net Core MVC

pandeygolu4200
 
on Oct 27, 2022 04:55 AM
496 Views

Hi,

I am uploading file from my mvc app and pas it to an api. While working in localhost mode it is working properly. But when i upload it through hosted api, it is returning 400 bad request. 

I do not know how to solve.

[HttpPost]
public IActionResult AddProviderDocument([FromForm] long DocumentTypeID, string DocumentName, string FileName, string DocumentNumber, long StateID, string IssueDate, string ExpirationDate, List<IFormFile>? Files)
{
    DateTime? dtstartdate = null;
    DateTime? dteradate = null;         
    if (IssueDate != null)
    {
        dtstartdate = DateTime.ParseExact(IssueDate.ToString(), "MM/dd/yyyy", CultureInfo.InvariantCulture);
    }
    if (ExpirationDate != null)
    {
        dteradate = DateTime.ParseExact(ExpirationDate.ToString(), "MM/dd/yyyy", CultureInfo.InvariantCulture);
    }
    if (ModelState.IsValid)
    {           
        ManageProviderDocumentModelCopy modeldoc = new ManageProviderDocumentModelCopy();
        modeldoc.UserID = TempData.Peek("ProviderId").ToString().ToUpper();
        TempData.Keep("ProviderId");
        //Convert.ToString(TempData.Peek("ProviderId")).ToUpper();
        modeldoc.DocumentTypeID = DocumentTypeID;
        modeldoc.DocumentName = DocumentName;
        modeldoc.DocumentNumber = DocumentNumber;
        modeldoc.StateID = StateID;
        modeldoc.IssueDate = dtstartdate;
        modeldoc.ExpirationDate = dteradate;
        modeldoc.Files = Files;
        modeldoc.FileName = FileName;
 
        HttpClient client = new HttpClient();           
        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer",HttpContext.Request.Cookies["Token"].ToString());
        byte[] data;
        
        foreach (var file in Files)
        {
            if (file.Length > 0)
            {
                using (var ms = new MemoryStream())
                {
                    file.CopyTo(ms);
                    data = ms.ToArray();
                    ByteArrayContent bytes = new ByteArrayContent(data);
                    MultipartFormDataContent multiContent = new MultipartFormDataContent();
                    multiContent.Add(bytes, "Files", modeldoc.FileName);
                    multiContent.Add(new StringContent(modeldoc.DocumentTypeID.ToString()), "DocumentTypeID");
                    multiContent.Add(new StringContent(modeldoc.DocumentName.ToString()), "DocumentName");
                    multiContent.Add(new StringContent(modeldoc.DocumentNumber.ToString()), "DocumentNumber");
                    multiContent.Add(new StringContent(modeldoc.StateID.ToString()), "StateID");
                    multiContent.Add(new StringContent(modeldoc.ExpirationDate.ToString()), "ExpirationDate");
                    multiContent.Add(new StringContent(modeldoc.IssueDate.ToString()), "IssueDate");
                    multiContent.Add(new StringContent(modeldoc.UserID.ToString()), "UserID");
                    var Response = client.PostAsync(new Uri("http://4.2.85.188:5026/api/provider/documentadd"), multiContent).Result;
                    //var Response = client.PostAsync(new Uri("http://localhost:5179/api/provider/documentadd"), multiContent).Result;
 
                }
            }
        }
        if (Response.StatusCode==200)
        {
            TempData["ProviderIds"] = modeldoc.UserID;
            TempData.Keep("ProviderIds");
            return Json(Response.StatusCode);
 
        }
        else
        {
            ModelState.AddModelError("CustomError", Response.ToString());
            return Json(Response);
        } 
    }
    else
    {
        return View();
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download