Submitting a File to a Server using Ionic

I am trying to upload a JSON/CSV file to the server and store it in a specific location similar to how I manually upload files using FileZilla. The goal is to be able to retrieve this file later on from a different user.

I attempted to use the http.post method as described in this guide but have not been successful so far.

If anyone can assist me with resolving this matter, it would be greatly appreciated.

Answer №1

To effectively store received data, a server is essential. Utilizing resources like the mentioned tutorial can guide you through this process.

The key adjustment required is for the PHP server to write the content from the post request onto disk. A snippet such as the one below should suffice:

Keep in mind that testing and potential modifications may be necessary.

<?php
    // Handling CORS 
    if (isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    
    }

    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers:       {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
    }


    $postdata = file_get_contents("php://input");
    if (isset($postdata)) {
        $request = json_decode($postdata);
        $myfile = fopen("newfile.json", "w") or die("Unable to open file!");
        fwrite($myfile, $request);
        fclose($myfile);
    }
    else {
        echo "Incorrect call!";
    }
?>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Obtain an array dynamically through a manufacturing plant

I'm currently working on extracting information from a factory and displaying it in a dynamic manner. Previously, I used the following approach: <div class="articlePage"> <h4> {{ posts[0].title }} </h4> <span style="color: # ...

How to store data retrieved with $http.get in AngularJS into a variable

I am attempting to assign data retrieved from $http.get to a variable in my controller. $http.get(URL).success(function (data) { $scope.results = data; console.log('results within $http.get :'+ $scope.results); }); console.lo ...

Transforming button click from EventEmitter to RXJS observable

This is the functionality of the component utilizing EventEmitter: import { Component, Output, EventEmitter } from "@angular/core"; @Component({ selector: "app-my-component", template: ` <button (click)="clickEvent($event)& ...

Shuffling Numbers in an Array After Removing an Element with AngularJS

I am working with a JSON array that contains tasks: tasks = [{taskcode:1, taskName:'abc'}, {taskcode:2, taskName:'abc1'}, {taskcode:3, taskName:'abc2'}, ..... ]; If I delete a task with the nam ...

Error: Unable to locate http-server command

I am encountering difficulties while attempting to launch an AngularJS project. The http server is refusing to start, even after trying various solutions previously suggested. Upon executing npm list --depth=0, the following error occurred in my project p ...

Retrieve JSON data from a WordPress site that is powered by WooCommerce, using basic authentication

I am in the process of creating an Android application that retrieves product data from a WordPress site. The website has the Woocommerce plugin installed. I have been able to successfully obtain the JSON data using Postman software and basic authenticat ...

Limit the radio button to being clickable

Ugh, I'm really struggling with this. I want to create a quiz where only the radio button is clickable, not the text itself. Can anyone help me figure out how to do this? I have two codes below that I don't quite understand. I'll include th ...

What is the best way to display asynchronous or observable data in Ionic framework?

I'm currently following a tutorial on LinkedIn Learning that covers implementing the design of a detail page with Ionic 4.0. The tutorial link can be found here. However, I've encountered an issue when trying to display the fetched data from the ...

In PHP, you can create a script that checks a specific value in a CSV column and then loops through

I'm currently facing challenges in finding the optimal solution to make this work efficiently. From an outdated system, I have a large CSV file as output and my goal is to convert it into a JSON file. The CSV contains several columns, with the ' ...

Retrieve information from a pop-up modal and showcase it in a table within the main controller

How can we transfer data from a modal to a main controller table and update the table on clicking the generate button in the modal? Currently, I am able to retrieve input text data from the generate button click event but the table is not being updated w ...

What is the best approach for implementing line coverage for object literal in Typescript Mocha unit-tests?

Lead: I am a newcomer to using typescript and writing unit tests with Mocha and Chai. Question: Can anyone provide tips on achieving 100% line coverage in unit tests for an object literal that isn't within a class? I want to avoid going static if pos ...

Encountering a missing value within an array

Within my default JSON file, I have the following structure: { "_name":"__tableframe__top", "_use-attribute-sets":"common.border__top", "__prefix":"xsl" } My goal is to add values by creating an array, but I am encountering an issue where my ...

Angular 7: struggling to locate the type declaration file

Everything seemed to be working fine with my project yesterday, but today I ran into an issue right after installing the ngx-pagination module: ERROR in src/app/views/dashboard/step1/step1.component.ts(1,23): error TS2688: Cannot find type definition ...

What is the process of transferring data from one div to another div (table) using AngularJS?

In my quest to enhance my table with JSON data upon clicking the + icon, I am faced with two sections: Pick Stocks where stock names and prices (retrieved from data.json) need to be added to the table found in the Manage Portfolio section. First Section h ...

Server-side node.js template merging for document files

Currently, I am on the lookout for a library that can assist me in merging a JSON object with a template (txt file) on the server side. It would be great if this library could also handle conditional statements such as if, greater than, equals, etc., as we ...

Is TypeScript React.SFC encountering incompatibility issues with types?

Trying to figure out TypeScript but struggling to get rid of these persistent errors. I've tried multiple approaches and resorted to using any wherever possible, but the errors still persist: (9,7): Type '(props: IRendererProps & { children? ...

Tips for incorporating include=JsonSerialize.Inclusion.NON_NULL into your @JsonSerialize annotation using the annox plugin

Our JAXB objects are being generated from an xsd using the maven plugin called maven-jaxb2-plugin. Here are the dependencies we are currently utilizing: jaxb2-basics - 0.6.2 jaxb2-basics-annotate - 0.6.2 We have included -Xannotate and -XtoString in ou ...

Issue with ionic $state.go function not properly redirecting or displaying any error messages

I've encountered an issue with the implementation of $state.go() in a simple function within my app. Despite using $state.go() successfully in other parts of the app, it is not working as expected in this particular function and I'm struggling to ...

What is the best way to implement server-side pagination using AngularJS's $resource feature?

I'm working on displaying a table with a large set of elements and I want to implement pagination to load only the elements shown on the current page. Currently, the JSON data is being loaded using $resource. After reading advice here, it seems like ...

"Error: The ajax call to the dojo endpoint is not retrieving the

Uncertain where I might have made a mistake as the code appears correct to me. I have confirmed that file.php -> mod_rewtite -> file.json is functioning properly by testing the JSON response through jQuery. However, the following code snippet immedia ...