Uploading an image using C# and WebRequest?

前端 未结 6 1833
名媛妹妹
名媛妹妹 2020-12-11 10:22

Here is the working code in Python (using cURL):

#!/usr/bin/python

import pycurl

c = pycurl.Curl()
values = [
          (\"key\", \"YOUR_API_KEY\"),
               


        
相关标签:
6条回答
  • 2020-12-11 10:48

    Try this:

    string file = @"C:\Users\Sergio\documents\visual studio 2010\Projects\WpfApplication1\WpfApplication1\Test\Avatar.png";
    string parameters = @"key=1df918979bf3f8dff2125c22834210903&image=" +
        Convert.ToBase64String(File.ReadAllBytes(file));
    
    0 讨论(0)
  • 2020-12-11 10:55

    Read image posted by in API

    public IHttpActionResult UpdatePhysicianImage(HttpRequestMessage request)
        {
            try
            {
                var form = HttpContext.Current.Request.Form;
                var model = JsonConvert.DeserializeObject<UserPic>(form["json"].ToString());
                bool istoken = _appdevice.GettokenID(model.DeviceId);
                if (!istoken)
                {
                    statuscode = 0;
                    message = ErrorMessage.TockenNotvalid;
                    goto invalidtoken;
                }
                HttpResponseMessage result = null;
                var httpRequest = HttpContext.Current.Request;
                if (httpRequest.Files.Count > 0)
                {
                    var docfiles = new List<string>();
                    foreach (string file in httpRequest.Files)
                    {
                        var postedFile = httpRequest.Files[file];
                        // var filePath = uploadPath + postedFile.FileName;
                        //  string fileUrl = Utility.AbsolutePath("~/Data/User/" + model.UserId.ToString());
                        string fileUrl = Utility.AbsolutePath("~/" + Utility.UserDataFolder(model.UserId, "Document"));
                        if (!Directory.Exists(fileUrl))
                        {
                            Directory.CreateDirectory(fileUrl);
                            Directory.CreateDirectory(fileUrl + "\\" + "Document");
                            Directory.CreateDirectory(fileUrl + "\\" + "License");
                            Directory.CreateDirectory(fileUrl + "\\" + "Profile");
                        }
                        string imageUrl = postedFile.FileName;
                        string naviPath = Utility.ProfileImagePath(model.UserId, imageUrl);
                        var path = Utility.AbsolutePath("~/" + naviPath);
                        postedFile.SaveAs(path);
                        docfiles.Add(path);
                        if (model.RoleId == 2)
                        {
                            var doctorEntity = _doctorProfile.GetNameVideoChat(model.UserId);
                            doctorEntity.ProfileImagePath = naviPath;
                            _doctorProfile.UpdateDoctorUpdProfile(doctorEntity);
                        }
                        else
                        {
                            var patientEntity = _PatientProfile.GetPatientByUserProfileId(model.UserId);
                            patientEntity.TumbImagePath = naviPath;
                            _PatientProfile.UpdatePatient(patientEntity);
                        }
                    }
                    result = Request.CreateResponse(HttpStatusCode.Created, docfiles);
                }
                else
                {
                    result = Request.CreateResponse(HttpStatusCode.BadRequest);
                }
            }
            catch (Exception e)
            {
                statuscode = 0;
                message = "Error" + e.Message;
            }
        invalidtoken:
            return Json(modeldata.GetData(statuscode, message));
        }
    
    0 讨论(0)
  • 2020-12-11 10:59

    Try changing :-

    "application/x-www-form-urlencoded"
    

    to

    "multipart/form-data"
    
    0 讨论(0)
  • 2020-12-11 11:01

    Shot in the dark, but maybe create an instance of Image, save the file to a Stream and use that to read the bytes into an array then upload it.

    As in:

    Image i = System.Drawing.Image.FromFile("wut.jpg");
    Stream stm = new Stream();
    System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
    System.Drawing.Imaging.EncoderParameters paramz = new System.Drawing.Imaging.EncoderParameters(1);
    myEncoderParameter = new EncoderParameter(myEncoder, 100L);
    paramz.Param[0] = myEncoderParameter;
    i.Save(stm, System.Drawing.Imaging.ImageFormat.Jpeg, paramz);
    /* I'm lazy: code for reading Stream into byte[] here */
    
    0 讨论(0)
  • 2020-12-11 11:08

    You should correctly form a multipart POST request. See an example here: Upload files with HTTPWebrequest (multipart/form-data)

    0 讨论(0)
  • 2020-12-11 11:09

    Try adding the content-type for the jpg into your multipart boundary.

    See this uRL for examples (at the end)

    http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

    0 讨论(0)
提交回复
热议问题