您好我有以下代码实际创建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
的内容。我出于某些原因不想使用HttpWebRequest
或HttpClient
。
答案 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);