Skip to main content
Inspiring
January 19, 2024
Answered

Script for downloading a file

Is it possible to create a jsx script to download a file?

This topic is closed to new replies. Start a new post to keep the conversation going.
Pontus HulinCorrect answer
Inspiring
January 29, 2024

I ended upp using this approach. My plugin is writte in angular.js. 

I use this .js code to download the file:

 

fac.downloadFile = function (url) {

var deferred = $q.defer();
return $http({
method: 'GET',
url: url,
responseType:'arraybuffer',
headers: {
'Content-Type': undefined
},
transformResponse: function (data) {
return data;
}
});
}

 

The I use the following code in jsx to save the file to disk:

 

function cargo_WriteFileToDisc(object) {
var string = unescape(object);
var errorString = "";
var docObject = JSON.parse(string);
var doc = app.activeDocument;
var imagePath = File(doc.filePath + "/" + docObject.filename);

if (docObject.imageData != null)
{
imagePath.open("w");
imagePath.encoding = "BINARY";
imagePath.write(docObject.imageData);
imagePath.close();
}
}

5 replies

Community Expert
January 23, 2024

Another option could be to create an invisible CEP extension and then send CSXS event from jsx to js for downloading the file.

-Manan

Translate
-Manan
Pontus HulinAuthorCorrect answer
Inspiring
January 29, 2024

I ended upp using this approach. My plugin is writte in angular.js. 

I use this .js code to download the file:

 

fac.downloadFile = function (url) {

var deferred = $q.defer();
return $http({
method: 'GET',
url: url,
responseType:'arraybuffer',
headers: {
'Content-Type': undefined
},
transformResponse: function (data) {
return data;
}
});
}

 

The I use the following code in jsx to save the file to disk:

 

function cargo_WriteFileToDisc(object) {
var string = unescape(object);
var errorString = "";
var docObject = JSON.parse(string);
var doc = app.activeDocument;
var imagePath = File(doc.filePath + "/" + docObject.filename);

if (docObject.imageData != null)
{
imagePath.open("w");
imagePath.encoding = "BINARY";
imagePath.write(docObject.imageData);
imagePath.close();
}
}

Translate
DTPtutorialyCZ-L.Zalesky
Inspiring
January 20, 2024

Another option for windows is Autohotkey script

One line code.

Example:

UrlDownloadToFile, http://someorg.org/archive.zip, C:\SomeOrg's Archive.zip

 

Translate
brian_p_dts
Community Expert
Community Expert
January 19, 2024

In addition to Peter's excellent suggestions, you can also write a curl string command and execute it via VB or AS depending on OS. 

Translate
Peter Kahrel
Community Expert
Community Expert
January 19, 2024

In ExtendScript you can use Kris Coppieters's 

https://coppieters.nz/?p=720

 

In UXP you can use one of the standard modules.

Translate