How to send multipart/form-data as parameter to api through Controller using httpwebrequest method

Viewed 10

I am uploading image files(IForm File) with additional data using JQuery Ajax, After Click on submit button The Controller is getting proper data . In My Controller I have implemented a method where I am passing the data, method type and url . It is working properly for data type application/json but for multipart/form-data Ienter image description here do not know how to pass data through the method to web api

Can Anyone help me the solve the issue. Below is my Controller. public IActionResult AddDocument([FromForm] DateTime ExpirationDate, List? Files) { ManageProviderDocumentModelCopy modeldoc = new ManageProviderDocumentModelCopy(); modeldoc.ExpirationDate = ExpirationDate; modeldoc.Files = (List?)Files;

            AccountApiCall accountApiCall = new AccountApiCall(Configuration["ABCAPI"]);
            var Response = accountApiCall.PostAPIcallFile<GetDataModel>(modeldoc, "abc/docadd", HttpContext.Request.Cookies["Token"].ToString());
            if (Response.status)
            {
                ViewBag.Id = Response.data;
                return View();

            }
            else
            {
                ModelState.AddModelError("CustomError", Response.message);
                return View();
            }

        else
        {
            return View();
        }

    }

Below is my Method for calling api. public T PostAPIcallFile(dynamic model, string APIURL, string token = "") { try { // var Data = JsonConvert.SerializeObject(model); var response = APIUtility.GetRequestFile("POST", "multipart/form-data", BaseURL + APIURL, token, "", model).GetResponse() as HttpWebResponse; return JsonConvert.DeserializeObject(APIUtility.UnPack(response));

        }
        catch (WebException ex)
        {
            string response = "";
            using (StreamReader r = new StreamReader(ex.Response.GetResponseStream()))
            {
                response = r.ReadToEnd(); // access the reponse message
            }
            if (!string.IsNullOrEmpty(response))
            {
                return JsonConvert.DeserializeObject<T>(response);
            }
            else
            {
                Responce responce = new Responce();
                responce.message = "Invalid Data";
                responce.status = false;
                var res = JsonConvert.SerializeObject(responce);
                return JsonConvert.DeserializeObject<T>(res);
            }
        }
    }

Below is my httpwebrequest method public static WebRequest GetRequestFile(string Method, string contentType, string endPoint, string Authorization = null, string Customer = null, dynamic content = null) {

        var request = (HttpWebRequest)WebRequest.Create(endPoint);
        request.Method = Method;
        request.ContentType = contentType;
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
        if (Authorization != null)
        {
            request.PreAuthenticate = true;
            request.Headers.Add("Authorization", "Bearer " + Authorization);
        }
        if (content != null)
        {             
            var dataArray = Encoding.UTF8.GetBytes(content);
            request.ContentLength = dataArray.Length;
            var requestStream = request.GetRequestStream();
            requestStream.Write(dataArray, 0, dataArray.Length);
            requestStream.Flush();
            requestStream.Close();
        }
        return request;
    }
0 Answers
Related