How to upload image via API to web server?

Where shall i include the image file to upload in server.

var request = (HttpWebRequest)WebRequest.Create ("http://*****.com/api/Hall/AddHall");
request.Method = "POST";
var postData = "StudentId=2109";

            var data = Encoding.ASCII.GetBytes (postData);
            request.Method = "POST";
            request.ContentType = "multipar/form-data";
            request.ContentLength = data.Length;

            request.Headers[HttpRequestHeader.Authorization] = "Bearer " + "2aoI5a7V74FrycIpR2fiMS0eTbcz0qATSft_HUUG0";



                using (var stream =  request.GetRequestStream ()) {
                    stream.Write (data, 0, data.Length);
                   var response = (HttpWebResponse)request.GetResponse ();

                var responseString = new StreamReader (response.GetResponseStream ()).ReadToEnd ();
                Console.WriteLine("this is the response:{0}",responseString);
Tagged:

Answers

  • CheesebaronCheesebaron DKInsider, University mod

    I would probably do something like:

    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + "2aoI5a7V74FrycIpR2fiMS0eTbcz0qATSft_HUUG0");
        using (var content = new MultipartFormDataContent("----MyBoundary"))
        {
            var data = Encoding.ASCII.GetBytes (postData);
            var fileContent = new ByteArrayContent(data);
            fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {
                FileName = "File Name"
            };
            content.Add(fileContent);
    
            var response = await client.PostAsync("http://*****.com/api/Hall/AddHall", content);
        }
    }
    
  • shaghayeghshaghayegh USMember ✭✭✭

    @JSparrow said:
    Where shall i include the image file to upload in server.

    var request = (HttpWebRequest)WebRequest.Create ("http://*****.com/api/Hall/AddHall");
    request.Method = "POST";
    var postData = "StudentId=2109";

              var data = Encoding.ASCII.GetBytes (postData);
              request.Method = "POST";
              request.ContentType = "multipar/form-data";
              request.ContentLength = data.Length;
    
              request.Headers[HttpRequestHeader.Authorization] = "Bearer " + "2aoI5a7V74FrycIpR2fiMS0eTbcz0qATSft_HUUG0";
    
          
    
                  using (var stream =  request.GetRequestStream ()) {
                      stream.Write (data, 0, data.Length);
                     var response = (HttpWebResponse)request.GetResponse ();
    
                  var responseString = new StreamReader (response.GetResponseStream ()).ReadToEnd ();
                  Console.WriteLine("this is the response:{0}",responseString);
              
    

    i have same problem. i cant know request.addfile("file",yourfile); what is yourfile?

Sign In or Register to comment.