Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.get('/', function(req, res){ //response to http server GET request at home url
- // when using this form to upload, no problems are experienced
- console.log('GET request at /');
- res.writeHead(200, {'Content-Type': 'text/html'});
- res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
- res.write('<input type="file" name="filetoupload"><br>');
- res.write('<input type="submit">');
- res.write('</form>');
- return res.end();
- });
- app.post('/fileupload', function(req, res){ //response to http server POST request at home url
- console.log("POST request received at /fileupload");
- res.writeHead(200, {'Content-Type' : 'text/html'});
- var form = formidable.IncomingForm();
- form.uploadDir = __dirname + "/temp"
- form.keepExtensions = true;
- form.parse(req, function(err, fields, files){
- console.log("Parsing...");
- if (err) {throw err};
- });
- form.on('file', function(name, file){ // the 'file' event is never emitted when receiving an upload from android...
- console.log("Uploaded: " + file.name);
- });
- form.on('end' , function(){
- console.log("Ending...");
- res.json();
- res.end();
- });
- form.on('progress', function(bytesReceived, bytesExpected){
- console.log((bytesReceived/bytesExpected) * 100 + " %");
- })
- form.on('error', function(err){
- console.log("There has been an error.");
- console.log(err);
- })
- });
- protected Boolean doInBackground(String... strings){
- //this asynchronously uploads a file that the user has selected
- // it returns true or false based on whether or not the upload was successful
- for (String string: strings) { // strings is an array of strings that represents the paths of the files the user selected
- try{
- URL url = new URL(urlString); //urlString is a string that hold's the URL of the server I am hosting at 'fileupload'
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setConnectTimeout(4000);
- //POST request header
- connection.setRequestMethod("POST");
- connection.setRequestProperty("USER-AGENT", "Mozilla/5.0");
- connection.setRequestProperty("Accept-Language", "en-US, en;q=0.5");
- connection.setDoOutput(true);
- DataOutputStream os = new DataOutputStream(connection.getOutputStream());
- BufferedReader outputReader = new BufferedReader(new FileReader(string));
- String outputLine;
- while ((outputLine = outputReader.readLine()) != null) {
- os.write(outputLine.getBytes());
- }
- os.flush();
- os.close();
- int responseCode = connection.getResponseCode();
- Log.i(TAG, "Response code: " + responseCode);
- if (responseCode != HttpURLConnection.HTTP_OK) {
- return false;
- }
- BufferedReader in = new BufferedReader(new InputStreamReader
- (connection.getInputStream()));
- String inputLine;
- StringBuilder response = new StringBuilder();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- in.close();
- Log.i(TAG, response.toString());
- connection.disconnect();
- } catch (IOException e) {
- Log.e(TAG, e.toString());
- return false;
- }
- }
- return true;
- }
Add Comment
Please, Sign In to add comment