Skip to content

Instantly share code, notes, and snippets.

@andris9
Created June 10, 2013 12:02
Show Gist options
  • Save andris9/5748245 to your computer and use it in GitHub Desktop.
Save andris9/5748245 to your computer and use it in GitHub Desktop.
Upload file
var inputName = "uploadedfile",
fileName = "test.txt",
fileContents = "hello world!", // UTF-8
targetUrl = "http://example.com/post.php"
require("fetch").fetchUrl(targetUrl, {
method: "post",
agent: false,
headers:{
"content-type": "multipart/form-data, boundary=AaB03x"
},
"payload": '--AaB03x\r\n'+
'content-disposition: form-data; name="' + inputName + '"; filename="' + fileName + '"\r\n'+
'\r\n'+
fileContents + '\r\n'+
'--AaB03x--'
}, function(err, meta, body){
if(err){
console.log("Error " + err.message);
return;
}
console.log("Staatus: " + meta.status);
if(body){
console.log(body.toString("utf-8"));
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment