I am trying upload image to server via HttpUrlConnection
but it's not working.
return internal server 500
anyone can help me? thats postman captures 1: http://i62.tinypic.com/6e279j.png 2: http://oi62.tinypic.com/6e279j.jpg
and that is my code block
url = new URL(urls[0]);
// POST header
File image = new File(outFile);
HttpURLConnection con = (HttpURLConnection)
url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Connection", "keep-alive");
con.setRequestProperty("ApiKey", ApiKey);
con.setRequestProperty("AuthKey", AuthKey);
con.setRequestProperty("Content-Disposition","form-data; name=\"fileUpload\"filename="+image.getName());
con.setRequestProperty("Content-Type","image/jpg");
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary8FAIDy4eUDwgPYCA");
// POST body (copied from file stream)
int i;
byte[] buff = new byte[8192];
FileInputStream fis = new FileInputStream(image);
OutputStream os = con.getOutputStream();
while((i = fis.read(buff)) == 8192)
os.write(buff);
os.write(buff, 0, i);
os.flush();
os.close();
con.connect();
Log.w("response",con.getResponseMessage()+" - " +con.getResponseCode() +" - "+ image.getName());
// Read response (still needed)
String text = null;
finally server side var _uploadDirectory = Path.Combine(pathProvider.GetRootPath(), "upImages"); var _filename = Helper.GetGUID();//Path.ChangeExtension(Helper.GetGUID(), "png");
if (Request.Files.Count() != 1)
return new NaberResponse("error", "invalid parameters");
try
{
if (!Directory.Exists(_uploadDirectory))
Directory.CreateDirectory(_uploadDirectory);
var _file = Request.Files.First();
using (var _fileStream = new FileStream(Path.Combine(_uploadDirectory, Path.ChangeExtension(_filename, "jpg")), FileMode.Create))
{
_file.Value.CopyTo(_fileStream);
}
return new NaberResponse(pData: new { media_url = string.Format("{0}/post/image/{1}", Request.Url.SiteBase, _filename) });