【发布时间】:2011-05-20 09:24:21
【问题描述】:
使用以下代码将图像上传到 imgur.com 会返回 http 400 错误代码。我的开发人员密钥是正确的,我尝试了不同的图像格式,大小最大为 70 kb。 我还尝试了http://api.imgur.com/examples 给出的 c# 代码示例,但它也给出了 http 400。 可能是什么问题?
public XDocument Upload(string imageAsBase64String)
{
XDocument result = null;
using (var webClient = new WebClient())
{
var values = new NameValueCollection
{
{ "key", key },
{ "image", imageAsBase64String },
{ "type", "base64" },
};
byte[] response = webClient.UploadValues("http://api.imgur.com/2/upload.xml", "POST", values);
result = XDocument.Load(new MemoryStream(response));
}
return result;
}
编辑:这是一个 ASP.NET MVC 应用程序,调用者控制器操作是:
[HttpPost]
public ActionResult UploadImage(HttpPostedFileBase uploadFile)
{
if (uploadFile.ContentLength > 0)
{
var imgService = new ImgUrImageService();
byte[] fileBytes = new byte[uploadFile.InputStream.Length];
Int64 byteCount = uploadFile.InputStream.Read(fileBytes, 0, (int)uploadFile.InputStream.Length);
uploadFile.InputStream.Close();
string fileContent = Convert.ToBase64String(fileBytes, 0, fileBytes.Length);
var response = imgService.Upload(fileContent);
}
return View();
}
【问题讨论】:
-
你有没有得到这个工作?
-
不,还是一样。这是我在家里做的一个副项目。我想我会看看其他图像服务。