1

I am trying to combine two urls into a single url.

let url = "http://example.com/public/";

and I have file in path "/samples/leads/Import_Leads_Sample.csv" which is coming from API. I am taking this in one subscript as filepath now filepath holds the

 filepath: "/samples/leads/Import_Leads_Sample.csv"

I want both this to be combined and form one URL as.

let url = "http://example.com/public/samples/leads/Import_Leads_Sample.csv"
Akash M
  • 488
  • 3
  • 6
  • 18

1 Answers1

0

Store the filepath in a variable and combine both variables.

let url = "http://example.com/public/";
let filepath = Object.filepath; // equals "/samples/leads/Import_Leads_Sample.csv"

let finalUrl = url + filepath.substr(1); // substr(1) to remove the '/' and avoid having a final url with '//' in it
SrAxi
  • 19,787
  • 11
  • 46
  • 65
  • `Object {filepath: "/samples/leads/Import_Leads_Sample.csv"}` this is how I am getting from API. And after following your steps I am getting as `http://example.com/public/[object Object]` – Akash M May 05 '17 at 08:22
  • Because in the variable `filepath` you have to store the string that you get from the API call. Check my update, now `filepath` equals the string url from your API response Object. – SrAxi May 05 '17 at 08:31
  • @AkashM check my answer's update. The thing is that you need to store the url in a variable as a string, not an object. So, navigate the object to retrieve the string and store it in a variable. That is what I have done in my answer, asuming the that the object that has the API's response is `Object`. But you did not provide full code or anything so I did my best with what I had. Cheers! ;) – SrAxi May 05 '17 at 08:54
  • Yes I stringify it using `JSON.stringify(obj);` and followed your steps and it worked. Thank you @SrAxi – Akash M May 05 '17 at 10:00
  • @AkashM I'm glad to help. It's always a huge pleasure being able to help others. Good luck in your project mate! There you have my +1! ;) – SrAxi May 05 '17 at 10:02
  • 1
    Thank you so much. I facing another issue which is also an part of this question. If you are interested in solving that please have look at it http://stackoverflow.com/questions/43736485/download-csv-file-with-angular-2-ionic-2 I am new to ionic2 and angular2. Thank you. – Akash M May 05 '17 at 10:24