I have a .pdf file client side that I would like to send in binary form to my server, which will be handling it with PHP.
Client side, I am using a POST request that looks like this:
var newFile = require("sdk/io/file");
var params = {};
params.log = newFile.read(filepath, "b");
var makeRequest = newRequest({
url: "SERVERURL",
headers: {
"Content-Type": "application/pdf"
},
content: params,
onComplete: function(response) {
console.error(response.text);
}
}).post();
On server side, I'm using
file_get_contents('php://input')
and file_put_contents to write to a path on the server.
I have tried base64_decode and urldecode on the input, neither of which have correctly regenerated the PDF on the server. With urldecode, I get a blank PDF with the correct number of pages, but no text.
I'm new to PHP, could someone help me out?
Thank you.
I do not know what all of your PHP code looks like but I would replace file_get_contents('php://input') with $_POST['log'].
Related
I want to add text to a text document using JavaScript and PHP. What would be the best way to do this?
This is possible by using Javascript (front-end) to send an ajax request to the PHP server script that does the operation (back-end).
What you can do is use jQuery.ajax or XMLHttpRequest.
XMLHttpRequest
var url = "addtext.php"; // Your URL here
var data = {
text: "My Text"
}; // Your data here
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(data));
jQuery.ajax
var url = "addtext.php"; // Your URL here
var data = {
text: "My Text"
}; // Your data here
$.ajax({
url: url,
data: data,
method: "POST"
})
Note: There is also the jQuery.post method, but I have not included it.
And, in the PHP file, with the necessary permissions, you can write to the file using fwrite in combination with the other file functions.
<?php
$text = $_POST["text"]; // Gets the 'text' parameter from the AJAX POST request
$file = fopen('data.txt', 'a'); // Opens the file in append mode.
fwrite($file, $text); // Adds the text to the file
fclose($file); // Closes the file
?>
If you want to open the file in a different mode, there is a list of modes on the PHP website.
All the filesystem functions can be found here on the PHP website.
I don't think you can append to a text document unless you are writing server side code.
There are some possible workarounds mentioned in this post:
Is it possible to write data to file using only JavaScript?
So currently I'm working on a simple project where users gets to upload an image to server. Before I mention my problem here is how I'm doing it:
Client:
var dataURL = sendingcanvas.toDataURL("image/*");
var imagedatatosend = dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
formdata = {
'image': imagedatatosend
};
$.ajax({
url: '../receive',
type: 'POST',
data: formdata,
encode: false,
success: function(result){
alert(result);
}
});
FYI: imagedatatosend size is lower than 5MB and contains exactly the file data selected.
Basically What happens Is that users select an image using <input type="file" tag Then I'm setting that selected file drawn in a canvas to convert it to base64 and send it to server.
Java Server:
String datareceived = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
byte[] bImg = Base64.decodeBase64(datareceived.getBytes("UTF-8"));
FileOutputStream fos = new FileOutputStream("hi.jpg");
fos.write(bImg);
fos.close();
I think I might not need to explain what the code above does. But I'm facing some serious performance problem I mean It takes some huge time to write the data to hi.jpg file, even if I try System.out.println(datareceived); It takes seconds for my mouse click to respond on server console.
I don't know why is it happening, Do I need to send the image data as multipart or what?
All replies are appreciated and Thanks in Advance:)
Use XMLHttpRequest and FormData to append the file. It will be sent using multipart and chunked appropriately.
See: https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects
i'm writing a Firefox addon on sdk, i need to use in my main function an array. this array contains a list of data located in my BD. that’s why im using a php file to connect the BD and echo data in JSON format . then i call back the php file using request function and finally parse the JSON to an array. (my problem is how to request the php file and parse the json to array ) i tryed
var Request = require("sdk/request").Request;
var function = Request({
url: "file.php",
onComplete: function (response) {
var arrray = response.json;
}
});
but that didn't work :/
i tried a lot of thing too .
could someone help me with the right function to request the php file then parse the json to array
`
I am trying to use BusinessObject RESTful API to download a generated (pdf or xls) document.
I am using the following request:
$.ajax({
url: server + "/biprws/raylight/v1/documents/" + documentId,
type: "GET",
contentType: "application/xml",
dataType: "text",
headers: {"X-SAP-LogonToken": token, "Accept": "application/pdf" },
success: function(mypdf) {
// some content to execute
}
});
I receive this data as a response:
%PDF-1.7
%äãÏÒ
5 0 obj
<</Length 6 0 R/Filter/FlateDecode>>
//data
//data
//data
%%EOF
I first assumed that it was a base64 content, so in order to allow the users to download the file, I added these lines in the success function:
var uriContent = "data:application/pdf; base64," + encodeURIComponent(mypdf);
var newWindow=window.open(uriContent, 'generated');
But all I have is an ERR_INVALID_URL, or a failure while opening the generated file when I remove "base64" from the uriContent.
Does anyone have any idea how I could use data response? I went here but it wasn't helful.
Thank you!
. bjorge .
Nothing much can be done from client-side i.e. JavaScript.
The server side coding has to be changed so that a url link is generated (pointing to the pdf file) and sent as part of the response. The user can download the pdf from the url link.
You cannot create file using javascript, JavaScript doesn't have access to writing files as this would be a huge security risk to say the least.
To achieve your functionality, you can implement click event which target to your required file and it will ask about save that file to user.
Recently i am learning json to create apps.I have a doubt in a Json , php based chat system .
In this , the code work fine for same origin policy.But for sending and receiving data from external url, it successfully sends data to external php.But not receiving any data from server.I search in internet to solve this problem , and found jsonp as alternative. I tried jsonp , but i m not sure if am correct because i am new to ajax itself.
Please don't mis understand my question.I want to load a index.html file from localhost , when i send request to external url (anysite.com/xx/ajax.php) .It process and returns the data back to index.html.But the problem is my data is sended finely and processed on the server but it doesn't return to remote file.But it works fine for same server.
$.tzPOST = function(action,data,callback)
{
$.post('http://anysite.com/xx/ajax.php?action='+action,data,callback,'json');
}
$.tzGET = function(action,data,callback){
$.get('http://anysite.com/xx/ajax.php?action='+action,data,callback,'json');
}
please help me with a code.
You cant receive JSON from external web by JavaScript, because of the policy.
But you can do AJAX request on your PHP file and there you can get the JSON by file_get_content http://cz2.php.net/file_get_contents function.
For using(working) with jsonp, u can take ready solution jquery-jsonp
from GitHub.
Example of using (by you question):
$.tzGET = function(action,data,callback){
var url = 'http://anysite.com/xx/ajax.php?action='+action;
$.jsonp({
type: 'GET',
url: url,
callbackParameter: callback,
dataType: 'jsonp',
data: data,
timeout: 10000,
success: function(json){
alert('success')
},
error: function(){
alert('error')
}
});