Upload Image on apache server - NetworkRequest

606
4
09-24-2020 03:37 AM
ManoKumar
New Contributor

Hi All,

I am working on a app which provide upload image feature on server but I am not able to upload images from app to server, I followed sample app (QuickReport->controls->FeatureServiceManager->function addAttachement()) but not able to upload images, I can send request to server php file but not getting images, I printed $_REQUEST and $_FILE in php file, both displaying empty. Kindly help me.

0 Kudos
4 Replies
ErwinSoekianto
Esri Regular Contributor

Manoj, 

I don't quite understand the entire issue. So, are you using the out of the box Quick Report template? or did you modify QuickReport->controls->FeatureServiceManager->function addAttachement()? Or is it a custom app with code that you get from the Quick Report template? if yes, maybe you can share the code snippets. 

Is this being uploaded to ArcGIS Online or Enterprise? Or going to  3rd party server?

In general, NetworkRequest is created so that you can send a request to ArcGIS Online or Enterprise easier with all the properties and settings set by default. You can take a look at this here,  https://developers.arcgis.com/appstudio/api-reference/qml-arcgis-appframework-networkrequest/  or use the Javascript XMLHttpRequest object, XMLHttpRequest - Web APIs | MDN 

Thank you,

Erwin

 

0 Kudos
ManoKumar
New Contributor

Hi Erwin,

I am using the QuickReport template's QuickReport/controls/FeatureServiceManager.qml file in my custom app to upload image on my server, I modified the the uploadAttachment function, here is code

function uploadAttachment(filePath, featureglobalId, callback, cacheI)
    {
        var targetUrl = "https://mysite.com/image_upload.php";
        var component = uploadAttachmentNetworkRequestComponent;
        var uploadAttachmentNetworkRequest = component.createObject(parent);
        uploadAttachmentNetworkRequest.url = targetUrl;
        uploadAttachmentNetworkRequest.callback = callback;
        var obj = {"file": "@"+filePath,"f": "json"};
        uploadAttachmentNetworkRequest.send(obj);
    }
    Here is my code where I am trying to attach image
            var attachments = [];
            var i = 0;
            var featureUid = AppFramework.createUuidString(0)
            
            var file_p   = '/Users/manoj/ArcGIS/AppStudio/Data/048f304c732c403191cba6a0411cdcc7.jpg';
            var fileInfo = AppFramework.fileInfo(file_p);
            var fileName = fileInfo.fileName
            var arr = fileName.split(".");
            var suffix = arr[1];
            var imageFilePath = fileInfo.filePath
            
            var sizeOfAttachment = app.getFileSize(file_p)
            
            attachments[i] = {"type":suffix,"size":sizeOfAttachment,"name":fileInfo.fileName,"filePath":file_p}
            
            featureServiceManager.uploadAttachment(imageFilePath, featureUid, function(errorcode, responseJson, fileIndex){
                                
                            }, i);
        }
I am not getting any file data on server.
could you please guide how can I upload image from my custom app to my server?

0 Kudos
ErwinSoekianto
Esri Regular Contributor

I see. I think it's a little bit tricky to debug it since it is going to your own server and your own PHP code. Those code are created to upload attachment specifically to ArcGIS Feature Service. I am not sure if your server and PHP code have the same specs. We are not using PHP in ArcGIS Feature Service. 

My recommendation would be to first try it using an application like the Postman and then maybe try building a simple independent javascript function that would do that using XMLHttpRequest, XMLHttpRequest - Web APIs | MDN  after that technically the same code that uses XMLHttpRequest should work inside AppStudio app. 

0 Kudos
ManoKumar
New Contributor

I tried XMLHTTPREQUEST to send data to my php file, it's working fine to upload

text data except image upload, I did not find any example or solution to upload image

from app to my php server by using xmlhttprequest, do you have any example

about this? or we can do this by any other way?

0 Kudos