How to call "asp.net api upload file" from php - php

I create a serive api using asp.net and a methoad POST upload a any file, and I want using code php call that api, but i don't known how.
Code asp.net api upload file as follows:
public Task<IEnumerable<string>> Post()
{
if (Request.Content.IsMimeMultipartContent())
{
string fullPath = HttpContext.Current.Server.MapPath("~/uploads");
MyMultipartFormDataStreamProvider streamProvider = new MyMultipartFormDataStreamProvider(fullPath);
var task = Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t =>
{
if (t.IsFaulted || t.IsCanceled)
throw new HttpResponseException(HttpStatusCode.InternalServerError);
var fileInfo = streamProvider.FileData.Select(i =>
{
var info = new FileInfo(i.LocalFileName);
return "File uploaded as " + info.FullName + " (" + info.Length + ")";
});
return fileInfo;
});
return task;
}
else
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "Invalid Request!"));
}
}
And class:
public class MyMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
{
public MyMultipartFormDataStreamProvider(string path)
: base(path)
{
}
public override string GetLocalFileName(System.Net.Http.Headers.HttpContentHeaders headers)
{
string fileName;
if (!string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName))
{
fileName = headers.ContentDisposition.FileName;
}
else
{
fileName = Guid.NewGuid().ToString() + ".data";
}
return fileName.Replace("\"", string.Empty);
}
}
So, how to I can call this api using code php ?
I referenced code to: http://www.codeguru.com/csharp/.net/uploading-files-asynchronously-using-asp.net-web-api.htm

To me it looks like you have a php website and looked on the web for an upload example and found a asp.net one.
If you got php running why not use php for the upload?

Related

How to upload Multiple images in one Request using Retrofit 2 and php as a back end?

I am making an app in which user can select multiple images and upload them to the server.
I am using PHP as a backend and retrofit2
I tried all answers on stackoverflow but still did not resolve it.
#Multipart
#POST("URL/uploadImages.php")
Call<Response> uploaImages(
#Part List< MultipartBody.Part> files );
code for sending files
Retrofit builder = new Retrofit.Builder().baseUrl(ROOT_URL).addConverterFactory(GsonConverterFactory.create()).build();
FileUploadService fileUploadService = builder.create(FileUploadService.class);
Call<Response> call = fileUploadService.uploadImages(list)
for (Uri fileUri : path) {
MultipartBody.Part fileBody = prepareFilePart("files", fileUri);
images.add(fileBody);
}
Call<Response> call=fileUploadService.uploadImages(images);
call.enqueue(new Callback<Response>() {
#Override
public void onResponse(Call<Response> call, Response<Response> response) {
Log.e("MainActivity",response.body().toString());
progressDialog.show();
}
#Override
public void onFailure(Call<Response> call, Throwable t) {
Toast.makeText(MainActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
Log.e("MainActivity",t.getLocalizedMessage());
progressDialog.dismiss();
}
});
}
here is my php code.
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
$count++; // Number of successfully uploaded file
}
}
}
}
Solution:
I figured out the problem ..I have to change the name of the MultipartBodt.Part from
"file" to "file[]".and receive them in $_FILES['file'] ... the same as you do with traditional form ... because I am sending the content as a form-data
so modify my preparFfile() method.
after searching and asking around, here is a full, tested and self-contained solution.
1.create the service interface.
public interface FileUploadService {
#Multipart
#POST("YOUR_URL/image_uploader.php")
Call<Response> uploadImages( #Part List<MultipartBody.Part> images);
}
and the Response.java
public class Response{
private String error;
private String message;
//getters and setters
}
2- uploadImages method
I pass a list of URI from onActivityResult() method, then I get the actual file path with the help of FileUtiles "the link to the class is commented"
//code to upload
//the path is returned from the gallery
void uploadImages(List<Uri> paths) {
List<MultipartBody.Part> list = new ArrayList<>();
int i = 0;
for (Uri uri : paths) {
String fileName = FileUtils.getFile(this, uri).getName();
//very important files[]
MultipartBody.Part imageRequest = prepareFilePart("file[]", uri);
list.add(imageRequest);
}
Retrofit builder = new Retrofit.Builder().baseUrl(ROOT_URL).addConverterFactory(GsonConverterFactory.create()).build();
FileUploadService fileUploadService = builder.create(FileUploadService.class);
Call<Response> call = fileUploadService.uploadImages(list);
call.enqueue(new Callback<Response>() {
#Override
public void onResponse(Call<Response> call, Response<Response> response) {
Log.e("main", "the message is ----> " + response.body().getMessage());
Log.e("main", "the error is ----> " + response.body().getError());
}
#Override
public void onFailure(Call<Response> call, Throwable throwable) {
Log.e("main", "on error is called and the error is ----> " + throwable.getMessage());
}
});
}
and the helper method used above
#NonNull
private MultipartBody.Part prepareFilePart(String partName, Uri fileUri) {
// https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
// use the FileUtils to get the actual file by uri
File file = FileUtils.getFile(this, fileUri);
//compress the image using Compressor lib
Timber.d("size of image before compression --> " + file.getTotalSpace());
compressedImageFile = new Compressor(this).compressToFile(file);
Timber.d("size of image after compression --> " + compressedImageFile.getTotalSpace());
// create RequestBody instance from file
RequestBody requestFile =
RequestBody.create(
MediaType.parse(getContentResolver().getType(fileUri)),
compressedImageFile);
// MultipartBody.Part is used to send also the actual file name
return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
}
3-My php code image_uploader.php:
<?php
$file_path = "upload/";
$full_path="http://bishoy.esy.es/retrofit/".$file_path;
$img = $_FILES['file'];
$response['message'] = "names : ";
if(!empty($img)){
for($i=0;$i<count($_FILES['file']['tmp_name']);$i++){
$response['error'] = false;
$response['message'] = "number of files recieved is = ".count($_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'][$i],"upload/".$_FILES['file']['name'][$i])){
$response['error'] = false;
$response['message'] = $response['message']. "moved sucessfully :: ";
}else{
$response['error'] = true;
$response['message'] = $response['message'] ."cant move :::" .$file_path ;
}
}
}
else{
$response['error'] = true;
$response['message'] = "no files recieved !";
}
echo json_encode($response);
?>

Uploading An Image As A ByteArray to a Blob using AS3 & AMFPHP

I'm currently having a bit of an issue when I try to upload a photo taking using an iOS camera to a MySQL database using PHP and unfortunately have been unable to find the right help online.
Basically The User takes a photo on their iOS device and I take that raw MediaPromise and put it into a ByteArray. I then call a PHP function using AMFPHP to add the binary to a Blob in my database. But when I test the whole thing, it never seems to work. Could somebody maybe help me with this problem or at least point me in the right direction? It would be highly appreciated. Here's the code:
AS3:
var dataSource:IDataInput;
function imageSelected1(event:MediaEvent) {
var imagePromise:MediaPromise = event.data;
dataSource = imagePromise.open();
if( imagePromise.isAsync ) {
var eventSource:IEventDispatcher = dataSource as IEventDispatcher;
eventSource.addEventListener( Event.COMPLETE, onDataComplete );
}
else {
readMediaData();
}
}
// PHP Connection //
// Database //
var gw2:NetConnection = new NetConnection();
gw2.connect(connectiongoeshere);
// Responder //
var pictureresponder:Responder = new Responder(onpicture);
function onpicture(pictureobj:Object) {
gotoAndStop(1);
}
function onDataComplete( event:Event ):void {
readMediaData();
}
function readMediaData() {
var imageBytes:ByteArray = new ByteArray();
//dataSource.readBytes( imageBytes );
dataSource.readBytes(imageBytes);
gw2.call("class.setData", pictureresponder, imageBytes);
}
PHP:
function setData($ba) {
// Connecting to the database //
mysql_pconnect("hi", "hi", "hi");
mysql_select_db("hi");
$result = mysql_query("UPDATE users set profilepicture2 ='$ba->data' WHERE email= 'email#gmail.com'");
return $result;
}

How to get blob image from mysql to as3 with php and addChild it in a way of getting byteArray and converting it to image?

How to get blob image from mysql to as3 with php and addChild it in a way of getting byteArray and converting it to image?
this is php:
if($RequestType == 'Select'){
while($row=mysql_fetch_assoc($results)){
$arrdata[]=$row;
}
foreach($arrdata as $key=>$value){
$output[$key] = $arrdata[$key];
$output[$key]['png'] = base64_encode($arrdata[$key]['png']);
}
header('Content-type: application/json');
print(json_encode($output));
i've already got something like this, but longer:
VBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAACXBIWXMAAC4jAAAuIwF4pT92AAAYxUlEQVR42u1bB3Sc1ZWe8k/vvfem6aPpGk2RZjQzGrVRl6xiyZZkWS4gFxyDGyEsxjE1gVBTSGAdQg4keyCxF4NsY1pCDARsBwiE4pBsIJjN7nKWgM3eJ7/hTDhUIyJyNj7nHh3P/DP/f79373e/e98b0oUXXkj6/2ykfwLwTwAW58br1q0j9fX1UQcGBpgjw0Pcy
(In this I'm assuming you have already handled the HTTP request result)
To transform a base64 encoded image to a DisplayObject, you first need to decode the Base64 to a ByteArray then use the Loader class to load the image content, listening for the Loader's Event.COMPLETE event to dispatch. Once you've got that event Loader.content will contain a DisplayObject that you can add to the screen using addChild().
Example: (EDIT: Added an Array to keep track of loaded objects in order they were requested and altered function to calls to cope)
private var countLoadedImages:int = 0;//track how many images have loaded
private var arrImageLoaders:Array = new Array();//store loaders for call back
public function loadImageFromBase64(yourBase64:String):void
{
var base64:Base64Decoder = new Base64Decoder();
base64.decode(yourBase64);//decode your image
var data:ByteArray = base64.toByteArray();//convert to byte array
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete);
arrImageLoaders.push(loader.contentLoaderInfo);//add to array in order func was called
loader.loadBytes(data);//hand over to Loader
}
protected function onLoadComplete(event:Event):void
{
countLoadedImages++;
event.target.removeEventListener(Event.COMPLETE,onLoadComplete);
if (countLoadedImages == arrImageLoaders.length)
{
allImagesLoaded();
}
}
protected function allImagesLoaded():void
{
var contentLoaderInfo:LoaderInfo
for (var x:int = 0; x<arrImageLoaders.length; x++)
{
contentLoaderInfo = arrImageLoaders[x] as LoaderInfo;
addChild(contentLoaderInfo.content);
}
}

HttpStatus 0 when writing file from AS3 to PHP using URLLoader

I'm doing a project in AS3 where the user can design something and order it after. The design is exported to a PDF, PNG and XML. These files I want to save on the server. On MAMP it all works and I get the HTTPStatus 200, note that I run my SWF from MAMP but the php-script is successfully accessed on the remote server and the files are written to the Remote server with succes as well. However when i copy the SWF to the remote server, the files do not save and I get a HTTPStatus 0.
In the class that I use (see AS3 code below) I use a URLLoader to get to the following one line php script:
<?php file_put_contents($_POST['location'] . $_POST['fileName'], base64_decode($_POST['fileData']));
Here the AS3 class:
public class ServerConnection extends Sprite
{
private var _files:Array = new Array();
private var _fileNames:Array;
private var _fileLocation:String = "";
private var _scriptLocation:String;
private var _fileSavesInProgress:Array = new Array();
public function ServerConnection(scriptLocation:String = null, files:Array = null, fileNames:Array = null)
{
if (scriptLocation) setScriptLocation(scriptLocation);
if (files) setFiles(files);
if (fileNames) setFileNames(fileNames);
}
public function encodeFiles():void {
for(var i:uint = 0; i < _files.length; i++) {
_files[i] = encodeFile(_files[i]);
}
}
public function encodeFile(byteArray:ByteArray):Base64Encoder {
var base64:Base64Encoder = new Base64Encoder();
base64.encodeBytes(byteArray);
return base64;
}
public function saveFiles(location:String = null):void {
if (location) setFileLocation(location);
for(var i:uint = 0; i < _files.length; i++) {
_files[i] = saveFile(_files[i], _fileNames[i]);
}
}
public function saveFile(encodedFile:Base64Encoder, fileName:String = "test"):void {
var data:URLVariables = new URLVariables();
data.fileData = encodedFile;
data.fileName = fileName;
data.location = _fileLocation;
this.dispatchEvent(new DebugEvent(DebugEvent.MESSAGE, "#serverConnection: a save file request was made for " +
fileName + "\n" +
"url: " + _fileLocation + "\n" +
" scriptLocation: " + _scriptLocation + "\n"
));
var request:URLRequest = new URLRequest(_scriptLocation);
request.method = URLRequestMethod.POST;
request.data = data;
var loader:URLLoader= new URLLoader();
loader.addEventListener(Event.COMPLETE, function(e:Event):void {fileSaved(loader);});
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatus);
loader.addEventListener(IOErrorEvent.IO_ERROR, ioError);
_fileSavesInProgress.push(loader);
try {
loader.load(request);
} catch (e:*) {
this.dispatchEvent(new DebugEvent(DebugEvent.MESSAGE, "#serverConnection: error:* = " + e + " \n"));
}
//navigateToURL(request);
}
public function displayObjectToPNG(displayObject:*, scale:Number = 1):ByteArray {
var bmpData:BitmapData=new BitmapData(displayObject.width, displayObject.height, true, 0xFFFFFF);
bmpData.draw(displayObject);
var byteArray:ByteArray = PNGEncoder.encode(bmpData);
return byteArray;
}
public function xmlToByteArray(xml:XML):ByteArray {
var byteArray:ByteArray = new ByteArray();
byteArray.writeUTFBytes(xml);
return byteArray;
}
public function setScriptLocation(url:String):void {
_scriptLocation = url;
}
public function setFileLocation(url:String):void {
_fileLocation = url;
}
public function setFiles(array:Array, encode:Boolean = true):void {
for each(var file:* in array) {
for each(var type:XML in describeType(file).extendsClass.#type) {
if (type == "flash.display::DisplayObject") file = displayObjectToPNG(file);
}
if (typeof(file) == "xml") {
file = xmlToByteArray(file);
}
_files.push(file);
}
if (encode) encodeFiles();
}
public function setFileNames(array:Array):void {
_fileNames = array;
}
// EVENTS
private function httpStatus(e:HTTPStatusEvent):void {
this.dispatchEvent(new DebugEvent(DebugEvent.MESSAGE, "#serverConnection: status = " + e.status + " \n"));
}
private function ioError(e:IOErrorEvent):void {
this.dispatchEvent(new DebugEvent(DebugEvent.MESSAGE, "#serverConnection: IOErrorID = " + e.errorID + " Message: "+ e.text + " \n"));
}
private function fileSaved(loader:URLLoader):void {
this.dispatchEvent(new DebugEvent(DebugEvent.MESSAGE, "#serverConnection: file save completed. " + (_fileSavesInProgress.length -1) + " files to go \n"));
_fileSavesInProgress.splice(_fileSavesInProgress.indexOf(loader), 1);
if (_fileSavesInProgress.length == 0) {
filesSaved();
}
}
private function filesSaved():void {
this.dispatchEvent(new DebugEvent(DebugEvent.MESSAGE, "#serverConnection: files saved \n"));
this.dispatchEvent (new ClassAttributesLoaded(ClassAttributesLoaded.CLASS_ATTRIBUTES_LOADED));
}
}
which I implement like this
var s:ServerConnection = new ServerConnection(
CONSTANT.SAVE_FILE_SCRIPT_LOCATION,
[currentTemplate.getXML(), exampleTemplate, pdf.save(Method.LOCAL)],
[CONSTANT.PACKAGE_ID + ".xml", CONSTANT.PACKAGE_ID + ".png", CONSTANT.PACKAGE_ID + ".pdf"]
);
s.addEventListener(DebugEvent.MESSAGE, writeToDebug);
s.addEventListener(ClassAttributesLoaded.CLASS_ATTRIBUTES_LOADED, exit);
s.saveFiles(CONSTANT.RELATIVE_FILE_DIRECTORY);
When I change loader.load(request); to navigateToURL(request); the code does work. on the local and remote server. for obvious reasons I can't use navigateToURL in this case. I do think that the fact that navigateToURL does work and loader.load(request) doesn't say something about the problem... but what?
I'm kind of stuck on this and would appreciate help.
Thanks in advance!
PS: for testing I have set the permission of the remote DIR to 777. Furthermore the files are successfully saved from MAMP to Remote anyhow as stated before.
Solution
After long research I have found the solution:
Adding the following crossdomain.xml to the root of my webserver (www.mysite.nl/).
<?xml version="1.0" ?>
<cross-domain-policy>
<site-control permitted-cross-domain-policies="master-only"/>
<allow-access-from domain="*.mydomain.com" to-ports="*"/>
<allow-http-request-headers-from domain="*.mydomain.com" headers="*"/>
</cross-domain-policy>
This is not secure so if you want to use this solution please change 'mydomain.com' to the specific domain you want to allow. So that fixed it.
How I got to the answer
I was able to catch the error using the following code:
loader.addEventListener(IOErrorEvent.IO_ERROR, handleSecurityError);
The error returned was the following: [SecurityErrorEvent type="securityError" bubbles=false cancelable=false eventPhase=2 text="Error #2048"]
Note that usually the error contains: [SecurityErrorEvent type="securityError" bubbles=false cancelable=false eventPhase=2 text="Error #2048: Security Sandbox Violation : http://www.domain-a.com/url/file-a.swf can not load data from http://www.domain-b.com/url/file-b.swf"]
This error is usually solved by loading a security policy. check out this link about how to use security policies: http://kb2.adobe.com/cps/142/tn_14213.html
However in this case the url accessed is within the same domain. (NOTE: same folder :D) So initially I thought this was irrelevant as there is no other domain contacted. But after searching for a week I got so desperate I tried it any how.
So adding a crossdomain.xml to the root of my server solved the problem.

AS2: load class variables with sendandload

I'm using Actionscript 2.0 in combination with PHP, now I can make a call to my PHP file and receive data but apparently I have to use that data immediately, I cannot use it to fill my class variables.
This is what I want :
class user {
var lastname:String;
function user(in_ID:Number){
var ontvang:LoadVars = new LoadVars();
var zend:LoadVars = new LoadVars();
zend.ID = in_ID;
zend.sendAndLoad("http://localhost/Services/getUser.php", ontvang, "POST");
ontvang.onLoad = function(success:Boolean) {
if (success) {
lastname = ontvang.lastname;
} else {
lastname = 'error';
}
};
}
}
I've found out that this is a big issue in AS2, I found this post to work around it if you're loading XML data but I can't seem to get it to work with LoadVars :
http://www.actionscript.org/forums/showthread.php3?t=144046
Any help would be appreciated ..
When your onLoad handler is called, it is being called as if it were a member function of the LoadVars instance, and not your user instance.
There are several ways around this, one is to use Delegate.create() to create a function which will work as intended, for example:
import mx.utils.Delegate;
class user {
var lastname:String;
var ontvang:LoadVars;
function user(in_ID:Number){
ontvang = new LoadVars();
var zend:LoadVars = new LoadVars();
zend.ID = in_ID;
ontvang.onLoad = Delegate.create(this, onLoad);
zend.sendAndLoad("http://localhost/Services/getUser.php", ontvang, "POST");
};
}
function onLoad(success:Boolean) : Void
{
if (success) {
lastname = ontvang.lastname;
} else {
lastname = 'error';
}
}
}
Don't forget that the load is asynchronous - when you create one of your user objects, the member variables won't be immediately available. What you may need to do is let your user object be capable of signaling its readiness much like LoadVars does, (e.g. with a callback function provided by the caller) so that your app is driven by by these asynchronous events.

Categories