How to upload a file using web services

Can anyone show me some example code of uploading a file using a web service?

For the moment it is a text file.

amazon s3 do this i believe

Thank you for your replay, I had a look at it. They are submitting the form as following:

<form enctype=“multipart/form-data”

Now I know that this works but the point is that I basically don’t have a web server, just web services. So instead of submitting a form I read the data, wrap it into a soap message and issue a web service call. I can add web services at will but I want to avoid having to program on the web server (the web service communicates with an application server).

So I guess what I need to do is to find out how to read the CONTENTS of the file into a variable so I can submit the data to the web service.

Any takers?


hi firstly that the form on my site you looked at, lol :slight_smile:

amazon s3 webservice is here

i havent looked into it myself yet, as we use secure ftp to transfer files in background, i imagine the overhead for a webservice would be massive

but i presume the binarry data could be base64 encoded (or something similar) into the SOAP responce?

i dont think this belongs in javascript forum

anyways i found thse, could be usefull, u got me interested now :slight_smile:

I guess he was trying to ask whether he can use Ajax to upload file to other web service provided by a site from different domain :slight_smile:

Surely I am working on an Ajax application and I communicate with my own application server thru web services. So I am in control of both sides.

I DO think this is a real JavaScript question, since all I want to find out is how I can read the contents of a file on the client machine into a JavaScript variable?

First let met tackle a simple text file (which is the case at hand now) and later I suppose I will want to add base64 encoding for binary files.

I will look at the examples provided, but they seem all POST requests but what I am looking for is the client-side JavaScript code.

I tried the following which is exactly what I want, but unfortunately I get access denied in both IE and Firefox:

var myRequest = new XMLHttpRequest;“GET”,“file:myfile.txt”,false);
var myText = myRequest.responseText;

you cannot get local file with “file:” protocol. It violates the rule to protect users’ security

So back to the drawing board. Any takers?

You cant use javascript to read the contents of a file into a variable. This poses several major security risks. You would have to get the file using a form input file, and then you would have access to the value of that, which you would then need to submit using the post method to the server. I hope that helps point you in the right direction.

OK I understand that and it makes sense (although the form method has access to local files as well but that aside).

In order to do so, is the only possibility to first submit the file to the server and then send it back to the client in order to process it in JavaScript, or can it be done directly? If so, how?

well if you going to go to the whole bother of uplaoding to the server why dont you let the server send the file as a webservice then? instead of sending it back

php on the server side is more capable than javascript on the client

here found another interesting article

php on the server side is more capable than javascript on the client

Well, it depends what it does first :smiley:

Well it seems I cannot stay away from PHP on the web server to solve the upload problem.

I am going to look into uploading the file to a PHP program on the server that copies it to my target server directory for subsequent processing by my normal application server through a web service command.

Thanks for the help.