kwemy94
kwemy94 330 XP
a posé

download file with React axios

Bonjour @all j'ai implementé l'impression de document en pdf via mon API Laravel. Le test avec posmant fonctionne très bien. Mais en consommant l'API avec react, un document téléchargé contient un document illisible n'ayant aucun trait avec le document attendu. comment y remedier?```

const getPDF = async () => {

1printToPDF(15).then(res => {
2 console.log(res.data);
3 const url = window.URL.createObjectURL(new Blob([res.data]));
4 console.log(url);
5 window.open(url, '_blank');
6
7}).catch(err => {
8 console.log(err.response);
9 toast.danger('Oups! Echec de téléchargent');
10})
1printToPDF(15).then(res => {
2 console.log(res.data);
3 const url = window.URL.createObjectURL(new Blob([res.data]));
4 console.log(url);
5 window.open(url, '_blank');
6
7}).catch(err => {
8 console.log(err.response);
9 toast.danger('Oups! Echec de téléchargent');
10})

}

kwemy94
kwemy94 330 XP
a répondu
1export const printToPDF = (sector_id, param=null)=>{
2 return axios.post(`${baseUrl}/client-download`, param, header, {responseType: 'blob'})
3}
1export const printToPDF = (sector_id, param=null)=>{
2 return axios.post(`${baseUrl}/client-download`, param, header, {responseType: 'blob'})
3}

const getPDF = async () => {

1 printToPDF(15).then(res => {
2 console.log(res.data);
3 const url = window.URL.createObjectURL(new Blob([res.data]));
4 console.log(url);
5 window.open(url, '_blank');
6
7 }).catch(err => {
8 console.log(err.response);
9 toast.danger('Oups! Echec de téléchargent');
10 })
11
12
13}
1 printToPDF(15).then(res => {
2 console.log(res.data);
3 const url = window.URL.createObjectURL(new Blob([res.data]));
4 console.log(url);
5 window.open(url, '_blank');
6
7 }).catch(err => {
8 console.log(err.response);
9 toast.danger('Oups! Echec de téléchargent');
10 })
11
12
13}
1
1
Confirmer la suppression

Êtes-vous sûr de vouloir supprimer cette réponse ? Cette action est irréversible.

Il faut Se connecter ou Créer un compte pour participer à cette conversation.

Confirmer la suppression

Êtes-vous sûr de vouloir supprimer ce sujet ? Cette action est irréversible.