To install edge.js on client side:
# npm install edge
Server side index.php - http://my.cent-dev.local:
<html>
<head>
<script src="https://code.jquery.com/jquery-2.2.2.min.js"></script>
<script>
$( document ).ready(function() {
$('#uploadBtn').on('click', function(event){
$.get( 'http://127.0.0.1:1337/', function( data ) {
console.log(data);
});
});
});
</script>
</head>
<body>
<p id="uploadBtn">Hit me to Upload</p>
</bodY>
</html>
Server side - upload.php:
<?php
file_put_contents('/tmp/debug1', print_r($_FILES, TRUE) . PHP_EOL, FILE_APPEND);
if (!empty($_FILES)) {
$dir = '/www/my/javascript/tmp/upload_dir/';
### Warning: remember to sanitize the filename because it could be forged.
move_uploaded_file($_FILES['file1']['tmp_name'], $dir . $_FILES['file1']['name']);
}
?>
Client side - running a Node.js web server that will upload the local files to the remote server:
const http = require('http');
const hostname = '127.0.0.1';
const port = 1337;
var url = require('url');
var edge = require('edge');
var uploadFile = edge.func(
{
source: function() {/*
using System;
using System.IO;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Printing;
using System.Net.Http;
public class Startup
{
public async Task<object> Invoke(dynamic input)
{
int a = (int)input.a;
int b = (int)input.b;
//MathHelper.printToPrinter(input.myMsg);
MathHelper.Upload(input.uploadURL, input.filePath);
return MathHelper.Add(a, b);
}
}
static class MathHelper
{
public static int Add(int a, int b)
{
return a + b;
}
public static System.IO.Stream Upload(string url, string filename)
{
Stream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
HttpContent fileStreamContent = new StreamContent(fileStream);
// Submit the form using HttpClient and
// create form data as Multipart (enctype="multipart/form-data")
using (var client = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
formData.Add(fileStreamContent, "file1", filename);
// equivalent to (action="{url}" method="post")
var response = client.PostAsync(url, formData).Result;
// equivalent of pressing the submit button on the form
if (!response.IsSuccessStatusCode)
{
return null;
}
return response.Content.ReadAsStreamAsync().Result;
}
}
public static void printToPrinter(string s) {
//string s = "string to print2";
PrintDocument p = new PrintDocument();
p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
{
e1.Graphics.DrawString(s, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));
};
try
{
p.Print();
}
catch (Exception ex)
{
throw new Exception("Exception Occured While Printing", ex);
}
}
}
*/},
references: ["System.dll", "System.Drawing.dll", "System.Net.Http.dll"]
});
var myTxt = "Hello World";
var remoteURL = 'http://my.cent-dev.local';
var uploadURL = remoteURL + "/javascript/tmp/upload.php";
var filePath = "C:/Users/bot/Downloads/tmp/node.js/asdf2.pdf";
http.createServer((req, res) => {
var queryData = url.parse(req.url, true).query;
console.log(queryData);
uploadFile({ a: 5, b: 10, myMsg: myTxt, uploadURL: uploadURL, filePath: filePath}, function (error, result) {
console.log(result);
});
res.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin': remoteURL,
});
res.end('Hello World123\n');
}).listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Reference:
http://tjanczuk.github.io/edge/#/
http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data
No comments:
Post a Comment