0

I am trying to save an image in server location following is my HTML code:

HTML code:

<form>
 <input type="file" accept="image/*"/>
</form>

Javascript

        var file    = document.querySelector('input[type=file]').files[0];
        var reader  = new FileReader();
        if (file) {
            reader.readAsDataURL(file);
        }
        reader.addEventListener("load", function () {
            $.ajax({
                type: 'POST',
                url: 'http://localhost/MyProject/src/images',
                data: {filename: file.filename, data: file.data},
                succes: function(response){
                    alert(response.responseText);
                },
                error: function(error) {
                    alert(error.responseText);
                }
            });
        }, false);

The success method of ajax is being called but on debugging I found that file.data is undefined. What variable should be used instead of file.data ?I need the uploaded image to be saved in my server

EDIT

I followed the answer in the mentioned duplicate question here is the new code

HTML:

<form id='uploadForm' enctype="multipart/form-data">
    <input name="file" type="file" id="fileName" />                 
    <button ng-click="signIn()">Upload</button>
</form>

Javascript:

var fileInput = document.getElementById('fileName');
        var file = fileInput.files[0];
        var form = document.getElementById('uploadForm');
        var formdata = new FormData(form);
        formdata.append('file', file)
            //Save image in serverBase
                $.ajax({
                    type: 'POST',
                    url: 'http://localhost/MyProject/src/images',
                    // Form data
                    data: formdata,
                    // Tell jQuery not to process data or worry about content-type
                    // You *must* include these options!
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function(response){
                        alert(response);
                    },
                    error: function(error) {
                        alert(error.responseText);
                    }
                });         

Using the above code my image is not getting saved in the mentioned 'http://localhost/MyProject/src/images' directory , although the ajax call is successful but the alert I get in response is some HTML code .Not sure whats happening. Any idea?

EDIT

On browsing some of the questions like this How to store uploaded file into server path using either javascript or jquery

It seems javascript being a client side scripting language isnt enough to store image on server path. I am using firebase for communicating with database . Do I have to include some other language too just to save data on my server?Does firebase have provisions to help me with the same?

Manas Saxena
  • 2,171
  • 6
  • 39
  • 58
  • 1
    When sending binary data in an AJAX request you should use a `FormData` object. See the duplicate question for more information – Rory McCrossan Aug 25 '17 at 14:50
  • @RoryMcCrossan That does answer how to send the ajax request , but i need to write the image in my server , currently I followed the answer in the duplicate question but with success ajax call but I am not the image is not uploaded on the server instead I see some HTML code returned in the response – Manas Saxena Aug 25 '17 at 19:54
  • Firebase provides cloud storage where I can store the uploaded pics, so I dont need to use any other language , firebase APIs would do the job for me thanks – Manas Saxena Aug 26 '17 at 12:22

0 Answers0