spring upload multiple file

spring upload multiple files

Spring upload multiple files with restful API

Spring upload multiple files with restful api is one of the most requires task. If you are working with spring, chances are high that you are working with file as well.

If you are doing registration, you might need to have multiple files to be uploaded to your server. So how to do spring upload multiple files is what I will be showing you.
Continue reading spring upload multiple files

facebook social marketing

Add batch/multiple posts to Facebook Page – Graph API

facebook graph API batch request usage

If you have or managing any facebook page, chances are you might be using faebook’s graph API. It comes with lots of flavors of languages like Java, PHP, Javascript.

Ok you know about that, this post is not about that. If you want to see how to make a post to facebook through graph API, look at this post https://gullele.com/using-facebook-api-post-articles-php/

This post is about making multiple posts to Facebook page.

Why would I need batch requests

Imagine you have 20 blog articles you want to post to facebook. If you are not using batch, then it means you are sending 20 post requests to facebook. And as you can imaging it is easy to hit the limit easily. Facebook will stop you from bombarding their site.

When you are using batch, you have only one request, holding all 20 posts. Yup, that is it. One request vs 20. Now you know why you have to use that.

How to send multiple blog posts or other posts to Facebook page at once

If you follow the above link I referenced, you will have an idea on how single post is doing. I will just continue on that example class and will add just one more method to handle batch request.

The main idea is, each blog or whatever post has to be an object of Request. And you will send all those requests wrapped in one and send it.


	/**
	 * Send batch request to FB than a single one
	 * @param $post_data
	 */
	private function batchPost(array $post_data, $token)
	{
		/*
		 * In the case of the batch request, each has to be an object of request. And all those can be 
		 * send as one big request.
		 */
		$batch_response = [];
		$requests = [];
		foreach ($post_data as $id => $data) {
			$requests[$id] = $this->facebook->request('POST', '/me/feed', $data);
		}

		$batch_response = $this->facebook->sendBatchRequest($requests, $token);

		return $batch_response;
	}

In the above example, the method will be accepting $post_data as a parameter. If a post example is considered, a single $data will look like


$data = [
			'link' => "https://gullele.com/link-goes-here",
			'message'=>"how to make batch request in facebook api",
			'title'=>"Making Multiple Requests in Facebook",
			'description' => 'In this article I will show you how you can make multiple blog posts to Facebook page with one single request.',
		];

Then the line with $this->facebook->request will create a request object and add it as part of request array. Also mind the $id, each should have unique key in the $request array.

Then Facebook will send you back the response with post id and other lots of information that you might want to keep it for later reference like to get metrics on the post – how many likes, impressions, shares.. which will help you to improve in the long run.

Let me know if you have any questions or other methods.

pass all the jars in classpath when compiling java

How to pass multiple jar files while using javac – compiling java classes

If you have single jar file, you can pass as: lets assume you are compiling file.java

javac path/to/file.java -classpath "path/to/jar/file.jar" -d classpath

But how about if you have multiple jar files to be included in the class path?

If you are on windows you can list the jar files separated by semi colon(;) and in unix you can pass with colon(:)

javac path/to/java/file.java -classpath "file1.jar:file2.jar:dir/*"

The last example is for unix environment but you can apply it to windows with proper path and semicolon

Select an element with multiple classes jQuery

CSS Classes, as we know are the main drivers in the UI. Since we have only one id per element, our arena or tweak on that area is almost limited.
But classes are possible to be deployed multiple times per element and we can apply independent actions per classes..
Now, when you have two classes assigned and you want to single out that element having two classes in jQuery you would do

$('.class1.class2')

Without any space in between.
Common usage would be with javascript where you would take and put classes based on actions.

Yup!

Multiple keyword search in multiple directories bash script

I guess the title is good enough :)
Probably you can do it in single grep command too.. but this way you will play with bash as well..

here is the bash script doing it


 #! /bin/bash
 dirs=(dir1 dir2 dir3 dir4 dir5)
 keywords=(keyword1 keyword2 keyword3 keyword4 keyword5)
 strs=""
 for word in "${keywords[@]}"; do
     strs+=" -e ${word} "
 done
 
 for path in "${dirs[@]}"; do
     dirname="$(basename "${path}")"
     grep -ir --color ${strs} ${path}
 done

Save this as finder.sh

and you can run it as bash finder.sh

See more on bash here

file upload

multiple file upload in Symfony2 example

Multiple File Upload in Symfony framework

I spent decent amount of time to put together the multiple file upload in symfony.

It is not that difficult actually but the problem is there is no direct/easy documentation or tutorial on it.

On the form type where you would build the form, have the files with the type of collection and make its type a file entity. In this case there would be two file types by default.

    public function buildForm(FormBuilderInterface $builder, array $options) 
    {
        /*
         * The file does not need to be added as "file" because it is referred
         * in the validation for File and SF will automatically know it is file.
         */
        return $builder
                ->add("files", 'collection', array(
		    'type'=>new FileType(),
		    'allow_add'=>true,
		    'data'=>array(new BundleEntityFile(),
		    new BundleEntityFile())
		))
                ->add('save', 'submit');
    }

    public function getName()
    {
        return "files";
    }
}

The trick is on adding the files as collection. Initially, we have two file inputs and their type being FileType.

The allow_add is important to be able to add files through javascript.

The javascript would look like:

/*Handling multiple picture upload*/
$('#add_pictures').click(function(){
    var total_files=$("#member_pictures_container li").length;
    var file_label=document.createElement("label");
    file_label.innerHTML="File";
    file_label.for="PhotoAndDescription_files_"+total_files;
    var div_file=document.createElement("div");    
    div_file.appendChild(file_label);
    var picture= document.createElement('input');
    picture.name="PhotoAndDescription[files]["+total_files+"][file]";
    picture.type="file";
    picture.id="PhotoAndDescripton_files_"+total_files;
    div_file.appendChild(picture);
    div_container=document.createElement('div');
    div_container.id="PhotoAndDescription_files_"+total_files;
    div_container.appendChild(div_file);
    var list_element=document.createElement('li');
    list_element.appendChild(div_container);
    $('#member_pictures_container').append(list_element);
});

FileType would look like:

class FileType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        return $builder->add("file", "file");
    }

    public function getName()
    {
        return "filetype";
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class'=>'BundleEntityFile',
            'csrf_protection'=>true,
            'csrf_field_name'=>'_token',
            'intention'=>'file'
        ));
    }
}

 

And the twig file would look like this one

{{form_start(form)}}
    {{ form_errors(form) }}
    {{ form_widget(form.about_member) }}
    • {% for file in form.files %}

 

  • {{ form_errors(file) }} {{ form_widget(file) }}

 

 

{% endfor %} {{form_end(form)}}

Pretty much this will take care of the multiple file handling in symfony2. As I mentioned earlier, it is not that difficult but the lack of documentation has made it so..

Do you know what to check to deploy Symfony app?

What is POST and HTTP-RAW-POST and php input has to do with enctype? Find out here