I need to create a HTTP request from JavaScript to PHP and send a big string to the server as a file. I can make this in Java, but I need it in JavaScript.
What I have in Java:
private static String serviceUrl = "http://www.site.com/page.php";
private static String boundary = " " + System.currentTimeMillis();
private static String line;
public static String execute(String params) throws IOException {
URL url = new URL(serviceUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
// Write request
// Request Heading
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
// Request Body
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write("--" + boundary + "\n");
out.write("Content-Disposition: form-data; name=\"xml\"; filename=\"small\"\n");
out.write("Content-Type: application/octet-stream\n");
out.write("\n");
out.write(params);
// Finishing request body
out.write("\n");
out.write("--" + boundary + "--\n");
out.flush();
out.close();
// Read response
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String response = "";
while ((line = in.readLine()) != null) {
System.out.println(line);
response = response + line;
}
in.close();
return response;
}
How can I turn this in JavaScript? Is it possible?
EDIT:
Hi, the purpose of this question was not to have the code rewritten.
The thing is I could solve the problem in Java, but I needed to solve it in javascript and had no idea where to start, I was actually looking for pointers, as the ones that were given here.
I apologize for not have written a clear question and thank the ones who understood it anyways.