C#LibCurlNet图像上传

时间:2018-05-13 09:19:36

标签: c# php curl

您好我有以下代码实际创建CurlFile Object并将图像发布到服务器。它在php上完美运行

$post_data = array('device_timestamp' => time());
$post_data['photo'] = "@".realpath('imagePath');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

但是我想用c#LibCurNet实现同样的目标。到目前为止我所尝试的是

var content = new MultipartFormDataContent();
content.Add(new StreamContent(File.Open(@"D:\mypictres\p.JPG", FileMode.Open)), "photo", "Image.JPG");
content.Add(new StringContent("mobiletime"), "1526137643");

easy.SetOpt(CURLoption.CURLOPT_POST, 
easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, content );

public class postData
{
        public string device_timestamp { get; set; }
        public string photo { get; set; }
}

使用as

 postData p = new postData();
 p.device_timestamp = "1526137643";
 p.photo = @"D:\mypictures\p.JPG";
 easy.SetOpt(CURLoption.CURLOPT_POST, 
 easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, p);

我真的很想知道c#中替换CurlFileObject的内容。我出于某些原因不想使用HttpWebRequestHttpClient

1 个答案:

答案 0 :(得分:1)

在我看来,我们是在同一条路上大声笑。请尝试以下代码。

MultiPartForm mf = new MultiPartForm();

mf.AddSection(CURLformoption.CURLFORM_COPYNAME, "name",
CURLformoption.CURLFORM_COPYCONTENTS, "value",
CURLformoption.CURLFORM_END);



mf.AddSection(CURLformoption.CURLFORM_COPYNAME, "photo",
CURLformoption.CURLFORM_FILE, @"yourpicturepath",
CURLformoption.CURLFORM_CONTENTTYPE, "multipart/form-data",
CURLformoption.CURLFORM_END);

easy.SetOpt(CURLoption.CURLOPT_HTTPPOST, mf);