Hi.
I’m trying to understend how can i upload files to be attached on a package create using REST API.
The creation of a package is OK. I’m using the REST API add-on and I’m able to create a packge without problems.
But I don’t know how to upload the files with this API.
Can someone help me with this one?
I’m doing a Java Client using Jersey – JAXRS.
Hello @jrpajr,
Hope you are well. Please check the Package Properties from here,
https://imsas.github.io/wpdm-rest-api-docs/#package-properties
As per other Attributes, you shall find files object, here you can bind id with file name. Thus you will be able to attach/upload file thereby. I hope you have understood.
Thank you and regards
Hello Nayeem.
I did not understood how I’m gona to attach the files.
I alread saw the “files” object and could not use it for uploading files, only to make reference to files that were alread uploaded previously (that are on the Media of Worpress).
The description of “files” object is: “Array of attached file names key by file ids.”
What kind of “file names” I’m supoused to put there? The local path of my PC? (like C:\TEMP\file_to_upload.jpg)
And what about the “id”? What kind of Id?
Do you have any example to give me?
I alread tried to put the local path there (like the above example), but the files were not uploaded to the server.
Here is a snippet code of what I’m doing:
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.Invocation.Builder;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
public class Test {
public String createPackage( final String name, final long categoryId, final String fileName ) {
try {
final Map params = new HashMap<>();
params.put( "title", name ); //$NON-NLS-1$
final List<Long> categoryIdList = new ArrayList<>();
categoryIdList.add( categoryId );
params.put( "categories", categoryIdList ); //$NON-NLS-1$
if ( null != fileName && ( !fileName.isEmpty() ) ) {
final List<String> files = new ArrayList<>();
files.add( fileName );
params.put( "files", files ); //$NON-NLS-1$
}
params.put( "status", "private" ); //$NON-NLS-1$ //$NON-NLS-2$
final List<String> access = new ArrayList<>();
access.add( "guest" );
params.put( "access", access ); //$NON-NLS-1$
final Entity entry = Entity.json( params );
final Client wpdmClient = ClientBuilder.newClient();
WebTarget wpdmPackagesTarget = wpdmClient.target( wpdmPackagesUrl );
String wpdmAuthorizationKey = "Bearer xxxxxxx"; //$NON-NLS-1$
final Builder builder = wpdmPackagesTarget.request( MediaType.APPLICATION_JSON ).header(
HttpHeaders.AUTHORIZATION, wpdmAuthorizationKey );
final JSONObject response = builder.post( entry, JSONObject.class );
return response.toJSONString();
} catch ( Exception e ) {
e.printStackTrace();
}
return null;
}
}
Hello @jrpajr,
Hope you are well. If you have already created a package. Then you will also be able to upload/attach files for that package. If you have kept just the file name you should keep the files in wp-content/uploads/download-manager-files
dir too. And for local files, you will not be able to attach files from the local PC drive directly, or you may please attach a live server file URL there. It will also help you upload files properly. please check and let me know then
Thank you and regards
Hey Nayeen.
Thanks for the attention.
Thats the main problem… I cant upload files using Java and REST API to this location.
This path (wp-content/uploads/download-manager-files), how can I, programmatically, upload files to it via REST API? Is it possible? Do yout have any code sample showing how to do it?
Hello @jrpajr,
Hope you are well. The code sample is here, the response code for any API should be like this,
The JSON response will look like the following example:
{
"id": 6488,
"title": "The Alchemist",
"slug": "the-alchemist",
"description": "The Alchemist follows the journey of an Andalusian shepherd boy named Santiago.",
"excerpt": "The Alchemist is a novel by Brazilian author Paulo Coelho.",
"author": 1,
"status": "publish",
"parent": 0,
"tags": [
"novel",
"paulo-coelho"
],
"categories": [
16
],
"thumbnail": "https://example.com/wp-content/uploads/2019/01/the-alchemist.jpg",
"additional_previews": [
"https://example.com/wp-content/uploads/2019/03/the-alchemist-cover.jpg",
"https://example.com/wp-content/uploads/2019/03/the-alchemist-map.jpg"
],
"version": "3.0.0",
"link_label": "Download",
"quota": 0,
"download_limit_per_user": "100",
"view_count": 80,
"download_count": 14,
"package_size": "811.84 KB",
"access": [
"guest",
"subscriber",
"administrator"
],
"user_access": [
"admin"
],
"individual_file_download": "1",
"cache_zip": "-1",
"template": "link-template-calltoaction1",
"page_template": "page-template-1col-flat",
"files": {
"1": "The Alchemist.pdf"
},
"fileinfo": {
"1": {
"title": "The Alchemist",
"password": "",
"license_price": {
"statdard": "",
"extended": "",
"unlimited": ""
},
"price": ""
}
},
"package_dir": "",
"publish_date": "2019-04-01 12:00 am",
"expire_date": "2019-08-31 12:00 am",
"terms_lock": "1",
"terms_title": "Agree to terms",
"terms_conditions": "You can't redistribute without prior authorization.",
"terms_check_label": "Checkbox Label",
"password_lock": "1",
"password": "[123][456]",
"password_usage_limit": "2",
"linkedin_lock": "1",
"linkedin_message": "LinkedIn Share Lock message",
"linkedin_url": "https://www.wpdownloadmanager.com/",
"tweet_lock": "1",
"tweet_message": "Custom tweet message",
"twitterfollow_lock": "1",
"twitter_handle": "Shahriar",
"facebooklike_lock": "1",
"facebook_like": "",
"email_lock": "1",
"email_lock_title": "Subscribe To Download",
"email_lock_msg": "",
"email_lock_idl": "0",
"icon": "https://example.com/wp-content/plugins/download-manager/assets/file-type-icons/arrow.png",
"base_price": "12.00",
"sales_price": "10.00",
"sales_price_expire": "2019-08-31 12:00 am",
"pay_as_you_want": "0",
"license": {
"statdard": {
"active": "1"
},
"extended": {
"price": "22",
"active": "1"
},
"unlimited": {
"price": "32",
"active": "1"
}
},
"discount": {
"subscriber": "10",
"contributor": "20",
"author": "30",
"editor": "40",
"administrator": "50"
},
"enable_license": "1",
"enable_license_key": "1",
"free_downloads": [
"/Applications/XAMPP/wpdm/wp-content/uploads/2019/04/The-Alchemist-free-sample.pdf"
],
"meta_input":{
"meta_key" : "Hello"
}
}
here file attach should be like this,
"files": {
"1": "The Alchemist.pdf"
}
And for this default format, the file should be residing in wp-content/uploads/download-manager-files
, thus the package should create with fa file. Please let me know if you have other queries
Thank you and regards
I’m not sure if I missed something… maybe I am…. because I still not able to upload files to this folder using a Java Client with REST API.
Maybe you are suggesting me to upload the files to wp-content/uploads/download-manager-files via FTP? Is that it?
Because reading your answer, I’m not able to find any clue about how to upload the files to this folder.
I’m sorry for beeing this persistent, but I’m realy need to understend how this works.
I was expecting some code like this to upload the files:
import java.io.File;
import java.io.IOException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
public class JerseyFileUpload {
private final static String contentType = "multipart/mixed";
public static void test(){
postFile("http://mypage.com/wp-json/wpdm/v1/package", "c:\\TEMP\\File.jpg");
}
public static void postFile(String serverURL, File imgFile) {
MultiPart multiPart = null;
try {
Client client = ClientBuilder.newBuilder().
register(MultiPartFeature.class).build();
WebTarget server = client.target(serverURL);
multiPart = new MultiPart();
FileDataBodyPart imgBodyPart = new FileDataBodyPart("imgFile", imgFile,
MediaType.APPLICATION_OCTET_STREAM_TYPE);
// Add body part
multiPart.bodyPart(imgBodyPart);
Response response = server.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(multiPart, contentType));
if (response.getStatus() == 200) {
String respnse = response.readEntity(String.class);
System.out.println(respnse);
} else {
System.out.println("Response is not ok");
}
} catch (Exception e) {
System.out.println("Exception has occured "+ e.getMessage());
} finally {
if (null != multiPart) {
try {
multiPart.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Hi @jrpajr,
I am not doing Java for many days, but I think this part is not dynamic for posting files,
public static void test(){
postFile("http://mypage.com/wp-json/wpdm/v1/package", "c:\\TEMP\\File.jpg");
}
can you please have a close look, if possible, please share some screenshots or test video or you can also, give your temporary wp-admin login details in a private reply for checking the issue
Thank you and regards
Hey Nayeem.
I didnt undertand what you asked.
Hi @jrpajr,
Files should reside in wp-content/uploads/download-manager-files
directory. Can you please share some screenshots or test videos or you can also, give your temporary wp-admin login details in a private reply for checking the issue
Thank you and regards
Hello,
I am trying to create a package with attached files using REST API. From the communication above, I understand that the files need to reside in
“wp-content/uploads/download-manager-files” (lets call it ‘root’) directory to be able to attach a file to a package. We have a requirement to store files in sub-directory of the above said root directory. When I try to create a package and attach a file from a sub-directory of the root using REST API, the package creation succeeds but doesn’t attach the file properly. Alternatively, when tried to attach it from the root folder it works.
I am wondering if there is a way to attach a file from sub-directory of the root, especially when this kind of scenario works from the UI.
Thanks in advance
Hello Product Registration,
Can you please open a separate topic for your issue, please?
Thank you and regards
The topic ‘How to upload the files of a package using REST API’ is closed to new replies.