Uploading a img/file to Quickbloxx Custom Objects API? - javascript

I'm working on QuickBloxx platform and had a use case where , I need to use some custom data models,I created a custom/class with one text and one file/img as datamodel fields.
I thoroughly followed the following url/documentationlink to quickbloxx custom objects files uploads:
For debug purpose I'm using Post to initiate POST request to Quickbloxx API as follows:
https://api.quickblox.com/data/coupon/file.json?token=mytoken , "coupon", was my object name.I'm able to POST text content to "customer_name" attribute but I cannot POST image to cust_img attribute.

You need to familiarize with this guide:
http://quickblox.com/developers/Javascript#Getting_started
{"errors":{"base":["Required session does not exist"]}} - need to create a session to avoid this error

Related

How do i use custom post types in separate JS file?

I have made a custom Wordpress post type recepies and I'm trying to use them in a separate JS file but can't find an easy way to access them? (I'm also using JQuery if that makes it easier?)
You can access the data of your custom post types in a javascript file using Rest API and jQuery.
For example, you want to display the ID of your recipe in a div with the class of "the_id" and the title of the recipe in a div with the class of "the_title". Moreover I'm assuming your post type has the name of "recipes" and we just want to get one recipe.
$(document).ready(function() {
$.ajax({
url: "http://yoururl.com/wp-json/wp/v2/recipes?per_page=1"
}).then(function(data) {
$('.the_id').append(data[0].id);
$('.the_title').append(data[0].title.rendered);
});
});
You can reduce the number of posts you are getting with the "per_page" parameter.
In data you can now access everything the Rest API gives you, in the example the ID (via data.id) and the title (via data.title.rendered).
This might interest you: https://developer.wordpress.org/rest-api/reference/
Here the fields you can access by default: https://developer.wordpress.org/rest-api/reference/posts/

Change content of file - Alfresco

I have an Custom Document Library Action to Alfresco files, and when I press this button opens a new page with an applet (javascript) to make changes to a file, but I'm doing the modifications in base64 and to "appear" on the screen with this :
var stringPDF = "<object data=\"data:application/pdf;base64," +
JSON.parse(pdfbase64).message + "\"
type=\"application/pdf\"width=\"100%\"
height=\"100%\"></object>";$("#pdfTexto").html(stringPDF);
But I really need is to change the file, for when the repository again, there have to change, not just display. How do I change the existing file's contents to the new with the change?
I use this URL to make GET of the file:
http://localhost:8080/share/proxy/alfresco/slingshot/node/content/workspace/SpacesStore/21384098-19dc-4d3f-bcc1-9fdc647c05dc/latexexemplo.pdf
Then I convert to the base64... And I make the changes...
But if I want to make a POST to change the content, how can I make this?
Thanks in advance.
As I mentionned in my response to this question :
The fastest and easiest way to achieve that is to leverage the RESTfull API
This will also ensure compatibility with new versions of alfresco.
Note that you need to provide the noderef for the document to update in the form property updatenoderef and that the property majorversion is a boolean flag to specify if the new version is a minor/major version of the document.
Here is a sample code that might help you with your usecase:
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost uploadFile = new HttpPost(<alfresco-service-uri>+"/api/upload?alf_ticket="+<al-ticket>);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("username", "<username>", ContentType.TEXT_PLAIN);
builder.addTextBody("updatenoderef", <noderef>, ContentType.TEXT_PLAIN);
builder.addTextBody("...", "...", ContentType.TEXT_PLAIN);
builder.addBinaryBody("filedata", <InputStream>, ContentType.DEFAULT_BINARY, <filename>);
HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);
CloseableHttpResponse response = httpClient.execute(uploadFile);
String responseString = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
JSONObject responseJson = new JSONObject(responseString);
if (response.getStatusLine().getStatusCode()!=200){
throw new Exception("Couldn't upload file to the repository, webscript response :" + responseString );
}
Note 1: You need to replace these tockens <*> with your own values/vars
Note 2: If you have problem retrieving a ticket, check this link, or this one
Note 3: To do this in JavaScript instead of java, visit this link and try to use js to post the parameters I referred as instructed !
Note 4: Since you are working on share, you are most probably authenticated.
If it is the case, you can access your alfresco repo through the proxy endpoint in share and all requests will have authentication ticket attached to them before getting forwarded to your repo !
In other terms, use this endpoint :
/share/proxy/alfresco/api/upload
Instead of :
/alfresco/service/api/upload
and You won't even have to attach a ticket to your requests.
You need to follow these steps to achieve what you are looking for.
1) Reading File:
To display content of PDF file already uploaded you need to read content of file. You are able to do it successfully using following API call.
http://localhost:8080/share/proxy/alfresco/slingshot/node/content/workspace/SpacesStore/21384098-19dc-4d3f-bcc1-9fdc647c05dc/latexexemplo.pdf
2) Capture New Content:
Capture new file content from User from applet. I guess you are storing it in some String variable.
3) Edit Existing File Content:
Issue here is that you cannot simply edit any pdf file using any of out of box Alfresco REST API (as far as I know). So you need to create your own RESTFul API which could edit pdf file's content. You can consider using some third party libraries to do this job. You need to plugin logic of editing pdf in RESTFul API
4) Changes back to Repo:
Call Your API from Step 3:
You could also have look at this plugins which could fulfill your requirements.
https://addons.alfresco.com/addons/alfresco-pdf-toolkit
Hope this helps.

Integration beetween a compositeComponent's attribute and a Servlet in JSF

I'm trying to integrate Plupload with JSF in the most independent way possible, for that I created a composite component.
To use Plupload in my JSF page i just call:
<comp:plupload ... value=#{MyBean.files} />
Where MyBean.files is a list.
When I add files to Plupload component and click the button "start upload" I want it to upload everything to a temporary folder and fill the object specified in "value" of my composite component with these files properties (path, for instance).
To upload the files i'm using a servlet, it has nothing to do with JSF, it works fine for the 1st part: it uploads everything to a temporary folder. My problem is the second part, I did a lot of research, but I can't find a way to communicate the attribute "value" in my JSF composite component to my servlet.
Plupload uses javascript to configure everything, the request will be sent to the URL specified in the attribute "url" in the following code:
<composite:interface>
...
<composite:attribute name="value" required="true" />
</composite:interface>
<composite:implementation>
...
<script type="text/javascript">
// Convert divs to queue widgets when the DOM is ready
$(function() {
$("#uploader").pluploadQueue({
// General settings
runtimes : '#{cc.attrs.runtimePreferences}',
url : '/plupload',
max_file_size : '#{cc.attrs.maxFileSize}',
...
});
});
</script>
<div id="uploader">
<p><h:outputText value="Your browser does not support this." /></p>
</div>
</composite:implementation>
I specified "/plupload" as url, that's my servlet's url (in web.xml).
I can think about two possible solutions:
Continue using this servlet, which is completely independent from FacesServlet, and find a way to pass the attribute "value" in my composite component as a request attribute to my servlet. But, how can I do this?
Stop using a new servlet, I should't do this since I'm using JSF, everything should be processed by FacesServlet. Instead of using a new servlet, i could create a ManagedBean. Inside this managedBean I could create a method and recover the HttpRequest and HttpResponse. It's easier to comunicate my compositeComponent with the method who handles the upload if it's a managedBean. Problem: How can i reference a managedBean's method through an URL? I still have to fill the attribute "url" in the javascript code. Is there anything like "/faces/MyBean?action='myMethod()'?
Thank you in advance for any answer ;)
Send it as request pathinfo.
url: '/plupload/' + encodeURIComponent('#{cc.attrs.value}'), // You might want to escape JS special characters like singlequotes, newlines, etc as well, depending on what the value can contain.
If the servlet is mapped on /plupload/* then you can obtain it as follows:
String value = request.getPathInfo().substring(1);

Convert many GET values to AJAX functionality

I have built a calendar in php. It currently can be controlled by GET values ​​from the URL. Now I want the calendar to be managed and displayed using AJAX instead. So that the page not need to be reloaded.
How do I do this best with AJAX? More specifically, I wonder how I do with all GET values​​? There are quite a few. The only solution I find out is that each link in the calendar must have an onclick-statement to a great many attributes (the GET attributes)? Feels like the wrong way.
Please help me.
Edit: How should this code be changed to work out?
$('a.cal_update').bind("click", function ()
{
event.preventDefault();
update_url = $(this).attr("href");
$.ajax({
type : "GET"
, dataType : 'json'
, url : update_url
, async : false
, success : function(data)
{
$('#calendar').html(data.html);
}
});
return false;
});
Keep the existing links and forms, build on things that work
You have existing views of the data. Keep the same data but add additional views that provide it in a clean data format (such as JSON) instead of a document format (like HTML). Add a query string parameter or HTTP header that you use to decide which view to return.
Use a library (such as YUI 3, jQuery, etc) to bind event handlers to your existing links and forms to override the normal activation functionality and replace it with an Ajax call to the alternative view.
Use pushState to keep your URLs bookmarkable.
You can return a JSON string from the server and handle it with Ajax on the client side.

JMVC jQuery.Model - cannot override a standard REST URL

I am using Javascript MVC models separately from JMVC (just as a jquery plugin).
Everything works fine, instead of overriding REST URLs. Look:
I am trying to override URL to retrieve data from server.
But it still tries to load data from URL "/Tags.json" instead of "/t.json".
$.Model.extend("Tag",
{
findAll: "/t.json" //overriding URL for findAll (just like in the manual)
});
$(function(){
Tag.findAll({}); //loads '/Tags.json' instead of 't.json'
})
Is it a bug?
$.Model("Tag",{
findAll : "/t.json"
},{})
You need to call $.Model not $.Model.extend
.findAll docs

Categories