2

I have implemented a code to download .doc file from server and as a server I am using node.js with express my server side code look like

function downloadresume(req, res, next)
{
  var data=req.body.data;
  console.log(data);
  try {
    var JsonData=JSON.parse(JSON.stringify(data));
    console.log(JsonData);
    var filename=JsonData.Id+".docx";
    var file=__dirname+"/Resumes/"+filename;
    fs.exists(file, function(exists) {
      if (exists) {
        var stats = fs.statSync(file);
        var mimetype = mime.lookup(__dirname+"/Resumes/"+filename);
        res.setHeader('Content-disposition', 'attachment; filename='+filename);
        res.setHeader('Content-type', mimetype);
        return res.download(file);
      } else {
       res.send([{error:'File not exists'}]);
      }
    });

  } catch (e) {
    console.log(e);
    res.send(e);
  }
}

and React Side code look like

  DownloadResume:function(id)
 {
   var postdata={
      data:{Id:id}
   };
   try {
     $.ajax({
       url:config.APIURL+"downloadresume",
       type: "POST",
       data:JSON.parse(JSON.stringify(postdata)),
        success: function (data, textStatus, jqXHR) {
           if(data !==null && typeof data[0] !== 'undefined')
           {
             swal("error",data[0].error,"error");
           }
        },
        error: function(xhr){
         swal("error",'Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText,"error");
     }
     });
   } catch (e) {
     swal("Eroor",e, "error");
   }
 },

it's not working if I will call downloadresume direct from browser then file is download successfully but when I tried it from React it's not working can anyone please guide me what's going wrong in above mentioned code.

I got the below mentioned response when I called it

Response Header

Response

Dhaval Patel
  • 7,471
  • 6
  • 37
  • 70
  • What's not working? Your react code is not doing anything with the server response (`data`). Are you ingesting the DOC and parsing it in the browser in some way, or are you wanting to just let the browser download it as a regular file (and if so, don't use XHR). – Andrew Mar 22 '16 at 06:06
  • @Andrew: I want just let browser download it as a regular file and I have removed XHR but in error function called – Dhaval Patel Mar 22 '16 at 06:18
  • @Andrew: See I have updated Response header and Response – Dhaval Patel Mar 22 '16 at 06:24
  • 2
    I'm sure there are browser extensions that will allow what you are trying to do, but XHR is not really for handling attachments. The typical way of doing this is to launch the URL either in the same window or in a popup. I'm not 100% sure if the navigation change will screw up anything in react or will behave identically in all browsers, so I would personally do the pop-out window (which will close itself as soon as the request resolves and the response starts streaming). See http://stackoverflow.com/questions/13929838/is-content-disposition-attachment-blocked-from-xmlhttprequest – Andrew Mar 22 '16 at 06:51
  • @Andrew: Thanks man for your help I will look into it – Dhaval Patel Mar 22 '16 at 07:05
  • No worries. I am going to vote to close as a duplicate, but I think you should keep this question (don't delete it). It will help future react devs. Good question. – Andrew Mar 22 '16 at 07:07

0 Answers0