Creating nodes from json including image field upload

I am trying to create a node programmatically from the data(JSON) sent by my front-end template.

<input id="title" name="title" type="text" > <input id="body" name="body" type="text"> <input type="file" name="pic" id ="pic1" accept="image/*"> 

^^ This is my front end html

jQuery(document).ready(function(){ jQuery("#save").click(function(){     //alert("The paragraph was clicked.");   var postData =  [{       "content-type" : "article",       "title": jQuery("#title").val(),       "body" : jQuery("#body").val(),        "pic1" : jQuery("#pic1").val(),   }];         jQuery.ajax({             type: "POST",             dataType: "application/json; charset=utf-8",             url: "my-module/create-artcle/", //This is the menu hook I created             data: {'article': postData},             success:function(data){             //if(data.status === "success"){               alert(data);             //  window.location = "/user";             //}           }     });   });  }); 

^^^ This is my json.

This data is sent to the hook_menu I created, which triggers a callback function to create the node. here is the code for that.

function create_article(){   global $user;    if(isset($_POST['article'])) {       $json = $_POST['article'];       $decodedData = json_decode($json, true);        $contentType = $decodedData[0]["content-type"];       $title = $decodedData[0]["title"];       $body= $decodedData[0]["body"];       $image1 = $decodedData[0]["pic1"];          //Create a article        $node = new stdClass();         //Set values to fields        $node->type = $contentType;        $node->title = $title;        $node->body[LANGUAGE_NONE][0]['value'] = $body;         node_object_prepare($node);        $node->language = LANGUAGE_NONE;         $node->uid = $user->uid;        $node->status = 1;          $node = node_submit($node);         node_save($node);   } } 

So far I am getting my code to work except saving the image, because I’m seriously confused in saving an image to my node. The value that I get from front-end is simply the name of the image. my question is : can I save an image to the node I am creating with what I am getting from front-end ? If so I am confused with this reference https://www.drupal.org/node/201594

could any one guide me to extend this code to save my image.. $image1 this variable contains the image name.

Thank you.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Creating nodes from json including image field upload

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.