skip to Main Content

Hello I Have a request in web form code behind and i like call web api send Object with a property of type IFormCollection, the object properties sending but file not

WebRequest wrqst = WebRequest.Create(URLService + method);
var postData = new StringBuilder();
                foreach (string key in form.Keys)
                {
                    postData.AppendFormat("{0}={1}&",
                        HttpUtility.UrlEncode(key),
                        HttpUtility.UrlEncode(form[key]));
                }

                var data = Encoding.ASCII.GetBytes(postData.ToString());

                wrqst.Method = "POST";
                wrqst.ContentType = "application/x-www-form-urlencoded";
                wrqst.ContentLength = data.Length;

                using (var stream = wrqst.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }

                
                WebResponse oResponse = wrqst.GetResponse();

I Receive file in Request.File
How i can send File?

2

Answers


  1. To send a file in your web API request, you need to use the multipart/form-data content type instead of application/x-www-form-urlencoded.

    Login or Signup to reply.
  2.     [HttpPost]
        [Route("api/WEB/Pos_PropertyImageSave")]
        [Route("api/POS/Pos_PropertyImageSave")]
        public HttpResponseMessage Pos_PropertyImageSave()
        {
            try
            {
                var request = HttpContext.Current.Request;
                bool IsUpload= false;
    
                /******************Image Upload*********************/
                if (request.Files["PropertyImage"] != null )
                {
                    obj.AttachemntName = request.Form["AttachemntName"] != null ? request.Form["AttachemntName"].ToString() : "";
                    obj.AttachemntNo = request.Form["AttachemntNo"] != null ? request.Form["AttachemntNo"].ToString() : "";
                    HttpPostedFile uploadImage = request.Files["PropertyImage"];
                    if (uploadImage.ContentLength > 0)
                    {
                        //Convert the File data to Byte Array which will be store in database
                        byte[] bytes;
                        using (BinaryReader br = new BinaryReader(uploadImage.InputStream))
                        {
                            bytes = br.ReadBytes(uploadImage.ContentLength);
                        }
    
                        filesInfo file = new filesInfo
                        {
                            File_Name = Path.GetFileName(uploadImage.FileName),
                            File_Type = uploadImage.ContentType,
                            File_Data = bytes
    
                        };
                        string FilePath = HttpContext.Current.Server.MapPath("~/Upload/") + Path.GetFileName(uploadImage.FileName);
                        File.WriteAllBytes(FilePath, bytes);
                        obj.AttachemntType = Path.GetExtension(uploadImage.FileName);
                        obj.AttachemntImagePath = "../Upload/" + Path.GetFileName(uploadImage.FileName); ;
                        obj.AttachemntImage = file.File_Data;
                        // obj.AttachemntName = Convert.ToBase64String(file.File_Data, 0, file.File_Data.Length);
    
    
                    }
                      IsUpload= true;
                    /*******************End Of Image Upload*************/
    
    
                }
    
    
                
    
                if (IsUpload)
                {
                    String sMessage = string.Format("Image has been " + obj.Mode + " successufully.");
                    Response responseclass = new Response(sMessage, sMessage, ((int)HttpStatusCode.OK), true);
                    HttpResponseMessage response = Request.CreateResponse<Response>(HttpStatusCode.OK, responseclass);
                    return response;
                }
    
                else 
                {
    
                    Response responseclass = new Response("", "Image Upload Failed", ((int)HttpStatusCode.NoContent), true);
                    HttpResponseMessage response = Request.CreateResponse<Response>(HttpStatusCode.OK, responseclass);
                    return response;
                }
                 
    
            }
            catch (Exception ex)
            {
    
                FailureResponse responseclass1 = new FailureResponse(ex.Message.ToString(), ((int)HttpStatusCode.BadRequest), false);
                HttpResponseMessage response1 = Request.CreateResponse<FailureResponse>(HttpStatusCode.OK, responseclass1);
    
                throw new HttpResponseException(response1);
            }
        }
    
       public class filesInfo
        {
            public string File_Name { get; set; }
            public string File_Type { get; set; }
            public byte[] File_Data { get; set; }
    
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search