Server code to accept file uploaded from intel XDK - php

Currently I'm creating an app using IntelXDK to upload image from devices to my server.
The problem currently I'm encountering is, how to code my backend so that it can receive the upload file from mobile device?
In PHP, I only know that the file upload requires:
<input type="file" name="file" />
then use $FILES["file"] to save it into storage
And is almost similar in .Net as well.
But I'm still couldn't think of how to receive the file once it is uploaded via mobile.
Would be great if someone share or advise the missing part (.Net and PHP).

In ASP.net server side use webservice receive in byte format and save that as you want.
Code sample refrence link http://www.codeproject.com/Articles/22985/Upload-Any-File-Type-through-a-Web-Service
[WebMethod]
public string UploadFile(byte[] f, string fileName)
{
// the byte array argument contains the content of the file
// the string argument contains the name and extension
// of the file passed in the byte array
try
{
// instance a memory stream and pass the
// byte array to its constructor
MemoryStream ms = new MemoryStream(f);
// instance a filestream pointing to the
// storage folder, use the original file name
// to name the resulting file
FileStream fs = new FileStream
(System.Web.Hosting.HostingEnvironment.MapPath
("~/TransientStorage/") +
fileName, FileMode.Create);
// write the memory stream containing the original
// file as a byte array to the filestream
ms.WriteTo(fs);
// clean up
ms.Close();
fs.Close();
fs.Dispose();
// return OK if we made it this far
return "OK";
}
catch (Exception ex)
{
// return the error message if the operation fails
return ex.Message.ToString();
}
}
}
}

For more information about uploading files to a server: https://software.intel.com/en-us/node/493213

If you are aware of ASP.NET Web API 2, have a look at this sample:
http://aspnet.codeplex.com/sourcecontrol/latest#Samples/WebApi/FileUploadSample/
Also, check these ones:
http://damienbod.wordpress.com/2014/03/28/web-api-file-upload-single-or-multiple-files/
http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2
http://www.c-sharpcorner.com/UploadFile/2b481f/uploading-a-file-in-Asp-Net-web-api/
Check these SO links:
How To Accept a File POST
File upload Jquery WebApi
I think, with above links, you will be surely able to create service that intakes file uploaded from a form.
Hope it helps you...
All the best...

Related

Failed to upload Azure Blob Storage in PHP

I use this official Azure Blob Storage sample file.
It shows how to upload HelloWorld.txt.
It is successfully uploaded. However, if the one point changes, for example HelloWorld.txt to Hello.jpg, it uploads 0 Byte file.
My code, phpQS.php is
// Create blob client.
$blobClient = BlobRestProxy::createBlobService($connectionString);
$fileToUpload = "Hello.jpg";
if (!isset($_GET["Cleanup"])) {
// Create container options object.
$createContainerOptions = new CreateContainerOptions();
...
}
Could you tell me how to upload a file completely?
This is the output in Webbrowser (with HTML).
Azure Console is
The sample project has issue, that is why, it doesn't work.
https://github.com/Azure-Samples/storage-blobs-php-quickstart/issues/6

Sending binary file content using JSON

I've written a REST interface for my ownCloud app. I've method getFileFromRemote($path) which should return a JSON object with the file content.
Unfortunately this only works when the file that I've specified in $path is a plaintext file. When I try to call the method for an image or PDF the status code is 200 but the response is empty. For returning the file contents I use file_get_contents for retrieving the content.
Note: I know ownCloud has a WebDAV interface, but I want to solve this with REST only.
EDIT
This is the code server side (ownCloud):
public function synchroniseDown($path)
{
$this->_syncService->download(array($path));//get latest version
$content = file_get_contents($this->_homeFolder.urldecode($path));
return new DataResponse(['path'=>$path, 'fileContent'=>$content]);
}
The first line retrieves downloades the content on the ownCloud server and works completely.
You probably have to base64_encode your file content to make json_encode/decode handle it properly:
return new DataResponse([
'path'=>$path,
'fileContent' => base64_encode($content) // convert binary data to alphanum
]);
and then when receiving file via second side, you will have to always:
$fileContent = base64_decode($response['fileContent']);
It's only one, but ones of easiest way to handle that. Btw, sooner or later you will find that Mime-Type would be useful in response.

How to paste image in chrome, then upload it to a server with PHP

I actually want to upload an image to a server.
To achieve this, i want the user just paste the image into chrome (the image is a print screen in fact), and then i post the stream to a php page, convert the stream as an image, and then upload it.
How can i achieve this web application ?
Today i have develop some differents parts :
I used this script, and i create the Upload.php page which gets the post variable and try to Create and image.
The problem i have, is that when i post the data, i only get a blob. I would like to get a base64 stream.
Can you help me ?
Thanks in advance.
I'm not sure why you are specifically looking for a "base 64 stream". If you are sending the Blob to your server via ajax, as far as your server is concerned, it's a file. Treat it no different than any other upload server-side. A Blob is a File without a name property. That's perhaps a bit overly-simplistic, but my point is that, again, this is really nothing more than a file as far as your server knows.
Assuming you are sending a multipart-encoded request, I'd like to point out that most user agents will set the filename property of the item's Content-Disposition header in the request to "blob" when the item you are uploading is a Blob instead of a file. It is possible to change this value in some browsers via the 3rd argument in FormData's append method, but I wouldn't rely on this just yet.
Also note that, if you are interested in a library that handles all of this already, I maintain, Fine Uploader which natively supports uploading images via paste in Chrome.
To answer this old question: Posting an image from clipboard with chrome is pretty much the same as posting a dropped file - except that the image/blob doesn't have the properties "name" and "lastModified".
var entry = items[i].webkitGetAsEntry();
if (!entry) entry = items[i].getAsFile();
if (entry instanceof Blob) /** CHROME pastet Bilder als Blob **/
{
entry.isFile = true;
entry.lastModifiedDate = new Date();
entry.name = ""+new Date().getTime()+"."+entry.type.split('/')[1];
}
if (entry.isFile)
{
//handle dropped file
}

PHP is_uploaded_file fails for iOS app

I'm writing the PHP/webserver side of an image upload for an iOS app. Using an html file I can upload an image to my script just fine. I've even written a Perl LWP script to post an image with no problems.
When the iOS app uploads an image it fails when I call is_uploaded_file. Sending back the $_FILES var in the json response show us what we expect for a file upload. Also I do a file_exists on the tmp_name and that fails as well.
Is there anything else I can look at in the request to determine what is going wrong? I'd like to be able to indicate what's wrong with the post request.
Here's the block of code where it stop processing the image upload:
//Check for valid upload
if(!is_uploaded_file($_FILES['photo']['tmp_name'])) {
$this->errors['upload_2'] = $photoErrorString;
$this->errors['files'] = $_FILES;
$this->errors['image_uploaded'] = file_exists($_FILES['photo']['tmp_name']);
error_log("uploadPhoto: upload_2");
return false;
}
As far as i know you cannot upload a image or photo from iOS app directly using PHP scripts.
You have to send 64 bit encode from the iOS app to the script responsible for the file upload as a simple POST. Then that script will firstly decode your photo's string and then PHP script will create the image from the string only to upload.
Code will be something like:
$filedb = $path_to_dir.$file_name_of_the_photo;
$profile_pic = $_POST['profile_pic'];
$profile_pic= base64_decode($profile_pic);
if(($img = #imagecreatefromstring($profile_pic)) != FALSE)
{
if(imagepng($img,$filedb))//will create image and save it to the path from the filedb with the name in variable file_name_of_the_photo
{
imagedestroy($img);// distroy the string of the image after successful upload
// do other updates to database if required
}
else //cannot create imagepng Error
{
//do what required
}
}
Hope this will work.
Linked below is a really nice NSData base64 category.
If you're unfamiliar with categories in Obj-C, they're basically stock objects that we know and love with some added methods (in this case, base64 encoding/decoding).
This class is really simple to drop into an existing project.
Enjoy.
http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html

How to save a snapshot of a SWF to the server with AS3?

I am facing the task of having to upload a snapshot to the server. But I don't want the user to download the image to their computer.
I have explored a few solutions of generating an image serverside with PHP, but they all seem to use a method where the server sends the image to the user.
See for instance: http://mattkenefick.com/blog/2008/11/06/saving-jpegs-with-flash/
I'm wondering if it's possible to save $GLOBALS["HTTP_RAW_POST_DATA"], which in that example contains the ByteArray sent by Flash, to the server as an image file....
Use php code that is along these lines to save the contents of $GLOBALS["HTTP_RAW_POST_DATA"]
// untested code
$imageBytes = $GLOBALS["HTTP_RAW_POST_DATA"]
// in real code you better create a new file for every upload :-)
$file = fopen("uploads/test.jpg", "w");
if(!fwrite($file, $imageBytes)){
return "Error writing to file: $file";
}
fclose($file);

Categories