Upload files with HTTPWebrequest (multipart/form-data)

后端 未结 21 2492
独厮守ぢ
独厮守ぢ 2020-11-21 04:53

Is there any class, library or some piece of code which will help me to upload files with HTTPWebrequest?

Edit 2:

I do not

21条回答
  •  误落风尘
    2020-11-21 05:34

    Not sure if this was posted before but I got this working with WebClient. i read the documentation for the WebClient. A key point they make is

    If the BaseAddress property is not an empty string ("") and address does not contain an absolute URI, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. If the QueryString property is not an empty string, it is appended to address.

    So all I did was wc.QueryString.Add("source", generatedImage) to add the different query parameters and somehow it matches the property name with the image I uploaded. Hope it helps

        public void postImageToFacebook(string generatedImage, string fbGraphUrl)
        {
            WebClient wc = new WebClient();
            byte[] bytes = System.IO.File.ReadAllBytes(generatedImage);
    
            wc.QueryString.Add("source", generatedImage);
            wc.QueryString.Add("message", "helloworld");
    
            wc.UploadFile(fbGraphUrl, generatedImage);
    
            wc.Dispose();
    
        }
    

提交回复
热议问题