Saving Binary Data (Files)

You can also save binary data (Files) like audio/video/images/txt etc in App42 cloud using File Upload Service. Whenever your app file is uploaded it is stored in our CDN network and can be accessed by its unique http end point url.

1. Saving/Uploading Files

You can save any binary file to App42 Cloud in few lines of code as shown below. File name will be used to uniquely refer to uploaded file. Once your file is uploaded, you will get an http end point for this file that you can use in your app for fetching it from App42 CDN network.

  • create User Api for Android
  • create User Api for J2ME
  • create User Api for Android
  • create User Api for iOS
  • create User Api for Java
  • create User Api for .NET
  • create User Api for Unity
  • create User Api for Ruby
  • create User Api for Rest
  •  create User Api for WP7/WP8
  • create User Api for Flash
String name = "MyPic";  
String userName = "Nick";  
String filePath = " file path from gallery /sd card";  
String description = "This is my holiday pic";    
// File will get uplaoded in App42 cloud with below snippet.                      
uploadService.uploadFileForUser(name,userName,filePath,UploadFileType.IMAGE,description new App42CallBack() {
	public void onSuccess(Object response)
	{
		Upload upload = (Upload)response;
		// This will have only single file uploaded above		
		ArrayList<Upload.File>  fileList = upload.getFileList();   
		for(int i = 0; i < fileList.size();i++ )  
		{  
			System.out.println("fileName is :" + fileList.get(i).getName());  
			System.out.println("fileType is :" + fileList.get(i).getType());  
			System.out.println("fileUrl is :" + fileList.get(i).getUrl());  
			System.out.println("Tiny Url is :"+fileList.get(i).getTinyUrl());
			System.out.println("fileDescription is: " + fileList.get(i).getDescription()); 
		}
	}
	public void onException(Exception ex) 
	{
		System.out.println("Exception Message"+ex.getMessage());	
	}
});
NSString *name = @"MyPic";  
NSString *name = @"Nick";  
NSString *filePath = @"file path from gallery /sd card";  
NSString *fileType = IMAGE;  
NSString *description = @"This is my holiday pic";    
Upload *upload = [uploadServiceObject uploadFileForUser:name userName:userName filePath:filePath uploadFileType:fileType description:description]; 
// File will get uplaoded in App42 cloud with above snippet.	returns the Upload object. */    
NSMutableArray *fileList =  upload.fileListArray;    // This will have only single file uploaded above 
for(File *file in fileList)  
{  
	NSLog(@"File Url is  %@" , file.url);  //This will return uploaded file url
}  
  	String fileName = "MyPic";
	String userName = "Nick";
	String description = "This is my holiday pic";	
	Stream inputStream = "Stream of file	";	/* Get result of photo chooser task */
    uploadService.UploadFileForUser(fileName,userName,inputStream,"Your File Type",description,requestCallback);  /* requestCallback points instance of App42Callback which overrides OnSuccess and OnException method */
	//Implemented request Callback method
	void OnSuccess(Object response)  
	{  
		Upload upload = (Upload) response;   
		IList<Upload.File> fileList =  upload.GetFileList();  // This will have only single file uploaded above
		for (int i = 0; i < fileList.Count; i++)  
		{  
			Console.WriteLine("fileUrl is " + fileList[i].GetUrl());  
			//This will return uploaded file url
		} 
	}   						
 
String name = "MyPic";  
String userName = "Nick";  
String filePath = " file path from gallery /sd card";
String description = "This is my holiday pic";                          
Upload upload = uploadService.uploadFileForUser(name,userName,filePath,UploadFileType.IMAGE,description); 
// File will get uplaoded in App42 cloud with above snippet.
Vector fileList =  upload.getFileList();    
for(int i=0;i < fileList.size();i++)  
{    
	Upload.File file = (Upload.File) fileList.elementAt(i);  
	System.out.println("File Url is " + file.getUrl()); //This will return uploaded file url	
}      
var fileName = "MyPic",
userName = "Nick",
description = "File Description",
filePath = " file path from gallery /sd card",  
result ;  
uploadService.uploadFileForUser(fileName,userName,filePath,"Your File Type",description,{  
	success: function(object) {  
	// File will get uplaoded in App42 cloud with above snippet.
		var uploadObj = JSON.parse(object);  
		result = uploadObj.app42.response.upload;
		console.log("result is " + result)
	},  
	error: function(error) {  
	}  
}); 
String name = "MyPic";  
String userName = "Nick";  
String filePath = "file path from gallery /sd card"; 
String description = "This is my holiday pic";                          
Upload upload = uploadService.uploadFileForUser(name,userName,filePath,UploadFileType.IMAGE,description); 
// File will get uplaoded in App42 cloud with above snippet.
ArrayList<Upload.File> fileList =  upload.getFileList(); // This will have only single file uploaded above   
for(Upload.File file : fileList)  
{  
	System.out.println("File Url is " + file.getUrl()); //This will return uploaded file url	
}  
String name = "MyPic";
String userName = "Nick";
String description = "This is my holiday pic";    
String filePath = "file path from gallery /sd card";
String fileType = UploadFileType.IMAGE; 
App42Log.SetDebug(true);        //Prints output in your editor console
uploadService.UploadFileForUser(name, userName, filePath, fileType, description, new UnityCallBack());
public class UnityCallBack : App42CallBack
{
    public void OnSuccess(object response)
    {
        Upload upload = (Upload) response;  
        IList<Upload.File>  fileList = upload.GetFileList();
        for(int i=0; i < fileList.Count; i++)
        {
            App42Log.Console("fileUrl is " + fileList[i].GetUrl());
		}
    }

    public void OnException(Exception e)
    {
        App42Log.Console("Exception : " + e);
	}
}
String name = "MyPic";  
String userName = "Nick";  
String filePath = "file path from gallery /sd card"; 
String fileType = "IMAGE";  
String description = "This is my holiday pic";
Upload upload = uploadService.UploadFileForUser(name,userName,filePath,fileType,description);  
// File will get uplaoded in App42 cloud with above snippet.	
IList<Upload.File> fileList =  upload.GetFileList();  // This will have only single file uploaded above
for (int i = 0; i < fileList.Count; i++)  
{  
	Console.WriteLine("fileUrl is " + fileList[i].GetUrl());  
	//This will return uploaded file url
}   
$name = "MyPic";  
$userName = "Nick";  
$filePath = "file path from gallery /sd card";  
$fileType = UploadFileType::IMAGE;  
$description = "This is my holiday pic";  						  
$uploadService  = $upload->uploadFileForUser($fileName,$userName, $filePath, $fileType, $description);
// File will get uplaoded in App42 cloud with above snippet.
$fileList = $upload->getFileList();  // This will have only single file uploaded above
foreach( $fileList as $file )  
{  
	Print_r("File Url is " . $file->getUrl());  
	//This will return uploaded file url
}  
name = "MyPic";  
userName = "Nick";  
filePath = "file path from gallery /sd card"; 
fType = App42::Upload::UploadFileType.new();  
uploadFileType = fType.enum("IMAGE");  
description = "This is my holiday pic";                            
upload = uploadService.upload_file_for_user(name,userName,filePath,uploadFileType,description); 
// File will get uplaoded in App42 cloud with above snippet.
fileList = Array.new();  
for file in fileList do  // This will have only single file uploaded above
	puts "url is #{file.url}";
	//This will return uploaded file url
end   
var name:String = "MyPic";    
var userName:String = "Nick";  
var description:String = "This is my holiday pic";      
var filePath:String = "file path from gallery /sd card"; 
var fileType:String = "<Your_file_type>"; 
uploadService.uploadFileForUser(name,userName, filePath,fileType,description,new callback());
class callback implements App42CallBack  
{  
	public function onException(exception:App42Exception):void  
	{  
		trace("Exception Message " + exception);  
	}  
	public function onSuccess(response:Object):void  
	{  
		var upload:Upload = Upload(response);
		if(upload.getFileList()!= null)
		{
			var fileList:Array  = upload.getFileList();				
			for(var k:int=0;k<fileList.length;k++)
			{
				var file:File=File(fileList[k]);
				trace("file url:"+file.getUrl());
			}
		}		
	}
}
N/A

2. Fetching Uploaded Files

Uplaoded file can be fetched using its file name and username. This will return the http end point of the file name passed.

  • create User Api for Android
  • create User Api for J2ME
  • create User Api for Android
  • create User Api for iOS
  • create User Api for Java
  • create User Api for .NET
  • create User Api for Unity
  • create User Api for Ruby
  • create User Api for Rest
  •  create User Api for WP7/WP8
  • create User Api for Flash
String name = "MyPic";  
String userName = "Nick";
// Following statement will fetch file info from App42 Cloud                        
uploadService.getFileByUser(name,userName, new App42CallBack() {
public void onSuccess(Object response) 
{
	Upload upload = (Upload)response;	
	// This will have only single file fetched as user can have only one file with given name	
	ArrayList<Upload.File>  fileList = upload.getFileList();   
	for(int i = 0; i < fileList.size();i++ )  
	{  
		System.out.println("fileName is :" + fileList.get(i).getName());  
		System.out.println("fileType is :" + fileList.get(i).getType());  
		System.out.println("fileUrl is :" + fileList.get(i).getUrl());  
		System.out.println("Tiny Url is :"+fileList.get(i).getTinyUrl());
		System.out.println("fileDescription is: " + fileList.get(i).getDescription());
	}	
}
public void onException(Exception ex) 
{
	System.out.println("Exception Message"+ex.getMessage());		
}
}); 
NSString *name = @"MyPic";  
NSString *name = @"Nick";    
Upload *upload = [uploadServiceObject getFileByUser:name userName:userName];
// Above statement will fetch file info from App42 Cloud  
NSMutableArray *fileList =  upload.fileListArray;    // This will have only single file uploaded above 
for(File *file in fileList)  
{  
	NSLog(@"File Url is  %@" , file.url);  //This will return uploaded file url
}  
String name = "MyPic";  
String userName = "Nick";  
uploadService.GetFileByUser(name,userName,requestCallback);  /* requestCallback points instance of App42Callback which overrides OnSuccess and OnException method */
// Above statement will fetch file info from App42 Cloud
//Implemented request Callback method
void OnSuccess(Object response)  
{  
	Upload upload = (Upload) response;   
	IList<Upload.File> fileList =  upload.GetFileList(); // This will have only single file fetched as user can have only one file with given name 
	for (int i = 0; i < fileList.Count; i++)  
	{
		Console.WriteLine("fileUrl is " + upload.GetFileList()[i].GetUrl());  //This will return file url info	 
	}
}   						
 
String name = "MyPic";  
String userName = "Nick";   
Upload upload = uploadService.getFileByUser(name,userName);
// Above statement will fetch file info from App42 Cloud
Vector fileList =  upload.getFileList();   // This will have only single file fetched as user can have only one file with given name 
for(int i=0;i < fileList.size();i++)  
{    
	Upload.File file = (Upload.File) fileList.elementAt(i);  
	System.out.println("File Url is " + file.getUrl()); 
	//This will return file url info
}      
var name = "MyPic",
userName = "Nick",
result ;  
uploadService.getFileByUser(name,userName,{  
	success: function(object) {  
	// Above statement will fetch file info from App42 Cloud
		var uploadObj = JSON.parse(object);  
		result = uploadObj.app42.response.upload;
		console.log("File Url is " + result.files.file.url)
	},  
	error: function(error) {  
	}  
}); 
String name = "MyPic";  
String userName = "Nick";                        
Upload upload = uploadService.getFileByUser(name,userName);
// Above statement will fetch file info from App42 Cloud
ArrayList<Upload.File> fileList =  upload.getFileList(); // This will have only single file fetched as user can have only one file with given name  
for(Upload.File file : fileList)  
{  
	System.out.println("File Url is " + file.getUrl()); 
	//This will return file url info
}  
String fileName = "MyPic";
String userName = "Nick";
App42Log.SetDebug(true);        //Prints output in your editor console
uploadService.GetFileByUser(fileName, userName, new UnityCallBack());
public class UnityCallBack : App42CallBack
{
    public void OnSuccess(object response)
    {
        Upload upload = (Upload) response;  
        IList<Upload.File>  fileList = upload.GetFileList();
        for(int i=0; i < fileList.Count; i++)
        {
            App42Log.Console("fileUrl is " + fileList[i].GetUrl());
        }
    }
    public void OnException(Exception e)
    {
        App42Log.Console("Exception : " + e);
    }
}
String name = "MyPic";  
String userName = "Nick";    
Upload upload = uploadService.GetFileByUser(name,userName);  
// Above statement will fetch file info from App42 Cloud	
IList<Upload.File> fileList =  upload.GetFileList();  // This will have only single file fetched as user can have only one file with given name
for (int i = 0; i < fileList.Count; i++)  
{  
	Console.WriteLine("fileUrl is " + fileList[i].GetUrl());  
	//This will return file url info
}   
$fileName = "MyPic";  
$userName = "Nick";                           
uploadService  = $upload->getFileByUser($fileName, $userName); 
// Above statement will fetch file info from App42 Cloud
$fileList = $upload->getFileList();  // This will have only single file fetched as user can have only one file with given name
foreach( $fileList as $file )  
{  
	Print_r("File Url is " . $file->getUrl());  
	//This will return file url info
}  
name = "MyPic";  
userName = "Nick";                           
upload = uploadService.get_file_by_user(name,userName);
// Above statement will fetch file info from App42 Cloud
fileList = Array.new();  
for file in fileList do  // This will have only single file fetched as user can have only one file with given name
	puts "url is #{file.url}";
	//This will return file url info
end   
var name:String =  "MyPic";  
var userName:String = "Nick"; 
uploadService.getFileByUser(name,userName,new callback());	
class callback implements App42CallBack  
{  
	public function onException(exception:App42Exception):void  
	{  
		trace("Exception Message " + exception);  
	}  
	public function onSuccess(response:Object):void  
	{  
		var upload:Upload = Upload(response);
		if(upload.getFileList()!= null)
		{
			var fileList:Array  = upload.getFileList();				
			for(var k:int=0;k<fileList.length;k++)
			{
				var file:File=File(fileList[k]);
				trace("fileList url:"+file.getUrl())
			}
		}		
	}
}
N/A

More Details?

bullet Go To [File Upload API Usage Guide] (http://api.shephertz.com/app42-docs/file-upload-service/)