0

i have a redux-from submission, console log look like this

{
  id: "x", 
  parentId: "b", 
  editing: true, 
  logo: FileList, 
}

Logo FileList contains my uploaded file name and file, i think

0: File(19355) {name: "11964821.jpeg", lastModified: ......

using fetch to send to nodejs, image contain my files and above data

function Update(image) {

    const requestOptions = {
        method: 'POST',
        headers: { ...authHeader(), 'Content-Type': 'application/x-www-form-urlencoded' },
        body: JSON.stringify(image)
    };
    return fetch(`${apiUrl}/API`, requestOptions).then(handleResponse)
    .then( data => {  return data; });
}

but this data not receiving nodejs, because is tried re.file etc.. its shows undefined

app.post('/image', upload.single('logo'), function (req, res, next) {

})


include formdata
  const onSubmit = (image, dispatch)=>{

    var formData = new FormData(); 
    formData.append('logo',image.logo[0]);

    var imageNew = {...image,formData};

    dispatch(imageActions.companyProfileLogoUpdate(imageNew)) ;  
  }

now log is

formData: FormData {}
id: "5c66478b0814720a4365fe72"
logo: FileList {0: File(19355), length: 1}
parentId: "5c66478b0814720a4365fe71"
david
  • 3
  • 4

4 Answers4

1
let formData = new FormData();
formData.append('logo' , {{your image file}}

// upload file as 
let result = await sendServerRequestForForm(url,formData);


function sendServerRequestForForm(url,data) {
var promise = new Promise(function(resolve) {
    resolve($.ajax({
        url,
        method : 'POST',
        data,
        processData: false,
        contentType: false,
        success: function(data) {
            return data
        },
        error: function (err) {
            try{
                let responseStatus = err.responseJSON
                // watch your response
            }catch (e) {
                console.log('Field to get response');
            }
        }
    }));
});
return promise

}
Basit
  • 862
  • 1
  • 12
  • 30
1

for fetch API you can try this

let formData = new FormData();
formData.append('logo', fileList[0]);

fetch(url, {
method: 'post',
body: data,
})
.then(…);
Basit
  • 862
  • 1
  • 12
  • 30
0

First thing is for using fetch api for post content-type header should match typeof body. since your body is json stringified your contentType header should be 'application/json' . more about this .

Second. since you want to upload an image which a file you should use FormData without requirement of setting contentType: like below:

let formData = new FormData();
formData.append('logo', image.logo[0]);

const requestOptions = {
  method: 'POST',
  body: formData
};

fetch(`${apiUrl}/API`, requestOptions)
.then(handleResponse)...
Gaurav Saraswat
  • 1,353
  • 8
  • 11
0

move formData to fetch file and remove header content type, Try this

function Update(image) {

var formData = new FormData(); 
formData.append('logo',image.logo[0]);  

    const requestOptions = {
        method: 'POST',
        headers: { ...authHeader() },
        body: formData
    };
    return fetch(`${apiUrl}/API`, requestOptions).then(handleResponse)
    .then( data => {  return data; });
}
Shanu T Thankachan
  • 993
  • 2
  • 8
  • 16