{"id":140,"date":"2017-10-02T09:17:36","date_gmt":"2017-10-02T07:17:36","guid":{"rendered":"https:\/\/numa-bord.com\/miniblog\/?p=140"},"modified":"2017-10-02T09:17:36","modified_gmt":"2017-10-02T07:17:36","slug":"jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata","status":"publish","type":"post","link":"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/","title":{"rendered":"jQuery : soumettre un formulaire contenant des fichiers gr\u00e2ce \u00e0 FormData"},"content":{"rendered":"<p>Aujourd&rsquo;hui il est possible d&rsquo;uploader des fichiers en ajax gr\u00e2ce \u00e0 <a href=\"https:\/\/developer.mozilla.org\/fr\/docs\/Web\/API\/FormData\">XMLHttpRequest Niveau 2 et l&rsquo;interface FormData<\/a> de javascript. Pour l&rsquo;utiliser avec la m\u00e9thode ajax de jQuery il suffit de lui passer comme donn\u00e9es l&rsquo;objet FormData et de configurer les options correctement comme ci-dessous.<\/p>\n<div class=\"codecolorer-container javascript default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\">&nbsp; &nbsp; <span class=\"co1\">\/\/au clic du bouton submit des formulaires ayant la classe &quot;form-ajax&quot;<\/span><br \/>\n&nbsp; &nbsp; $<span class=\"br0\">&#40;<\/span>document<span class=\"br0\">&#41;<\/span>.<span class=\"me1\">on<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;submit&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">&quot;.form-ajax&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"kw1\">function<\/span> <span class=\"br0\">&#40;<\/span>e<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; e.<span class=\"me1\">preventDefault<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><span class=\"co1\">\/\/on bloque l'envoie classique<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">var<\/span> formData <span class=\"sy0\">=<\/span> <span class=\"kw1\">new<\/span> FormData<span class=\"br0\">&#40;<\/span>$<span class=\"br0\">&#40;<\/span><span class=\"kw1\">this<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#91;<\/span><span class=\"nu0\">0<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><span class=\"co1\">\/\/r\u00e9cup\u00e9ration des donn\u00e9es du formulaire dans l'objet FormData<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/appel ajax jQuery<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; $.<span class=\"me1\">ajax<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url<span class=\"sy0\">:<\/span> <span class=\"st0\">'\/ma\/route\/ajax'<\/span><span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;POST&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data<span class=\"sy0\">:<\/span> formData<span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; processData<span class=\"sy0\">:<\/span> <span class=\"kw2\">false<\/span><span class=\"sy0\">,<\/span><span class=\"co1\">\/\/ne pas oublier cette option<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentType<span class=\"sy0\">:<\/span> <span class=\"kw2\">false<\/span><span class=\"sy0\">,<\/span><span class=\"co1\">\/\/ne pas oublier cette option<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error<span class=\"sy0\">:<\/span> <span class=\"kw1\">function<\/span> <span class=\"br0\">&#40;<\/span>msg<span class=\"sy0\">,<\/span> string<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/gestion des erreurs<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success<span class=\"sy0\">:<\/span> <span class=\"kw1\">function<\/span> <span class=\"br0\">&#40;<\/span>response<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/gestion de la r\u00e9ponse<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>De cette mani\u00e8re vous pouvez traiter cot\u00e9 serveur le formulaire de la m\u00eame mani\u00e8re que si il n&rsquo;\u00e9tait pas en ajax.<\/p>\n<p>Pour un exemple de traitement d&rsquo;upload d&rsquo;image cot\u00e9 serveur en php vous pouvez vous r\u00e9f\u00e9rer a <a href=\"https:\/\/numa-bord.com\/miniblog\/snippet-php-uploader-image-provenance-dun-formulaire\/\">cet article<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Aujourd&rsquo;hui il est possible d&rsquo;uploader des fichiers en ajax gr\u00e2ce \u00e0 XMLHttpRequest Niveau 2 et l&rsquo;interface FormData de javascript. Pour l&rsquo;utiliser avec la m\u00e9thode ajax de jQuery il suffit de lui passer comme donn\u00e9es l&rsquo;objet FormData et de configurer les options correctement comme ci-dessous. &nbsp; &nbsp; \/\/au clic du bouton submit des formulaires ayant la [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-140","post","type-post","status-publish","format-standard","hentry","category-developpement","category-javascript","category-jquery"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>jQuery : soumettre un formulaire contenant des fichiers gr\u00e2ce \u00e0 FormData - Pense b\u00eate d&#039;un d\u00e9veloppeur web<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/\" \/>\n<meta property=\"og:locale\" content=\"fr_FR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"jQuery : soumettre un formulaire contenant des fichiers gr\u00e2ce \u00e0 FormData - Pense b\u00eate d&#039;un d\u00e9veloppeur web\" \/>\n<meta property=\"og:description\" content=\"Aujourd&rsquo;hui il est possible d&rsquo;uploader des fichiers en ajax gr\u00e2ce \u00e0 XMLHttpRequest Niveau 2 et l&rsquo;interface FormData de javascript. Pour l&rsquo;utiliser avec la m\u00e9thode ajax de jQuery il suffit de lui passer comme donn\u00e9es l&rsquo;objet FormData et de configurer les options correctement comme ci-dessous. &nbsp; &nbsp; \/\/au clic du bouton submit des formulaires ayant la [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/\" \/>\n<meta property=\"og:site_name\" content=\"Pense b\u00eate d&#039;un d\u00e9veloppeur web\" \/>\n<meta property=\"article:published_time\" content=\"2017-10-02T07:17:36+00:00\" \/>\n<meta name=\"author\" content=\"Numa\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u00c9crit par\" \/>\n\t<meta name=\"twitter:data1\" content=\"Numa\" \/>\n\t<meta name=\"twitter:label2\" content=\"Dur\u00e9e de lecture estim\u00e9e\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/\"},\"author\":{\"name\":\"Numa\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/#\/schema\/person\/f9d00acd1703f17e5a6895283eb46a7e\"},\"headline\":\"jQuery : soumettre un formulaire contenant des fichiers gr\u00e2ce \u00e0 FormData\",\"datePublished\":\"2017-10-02T07:17:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/\"},\"wordCount\":193,\"commentCount\":0,\"articleSection\":[\"D\u00e9veloppement\",\"Javascript\",\"jQuery\"],\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/\",\"url\":\"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/\",\"name\":\"jQuery : soumettre un formulaire contenant des fichiers gr\u00e2ce \u00e0 FormData - Pense b\u00eate d&#039;un d\u00e9veloppeur web\",\"isPartOf\":{\"@id\":\"https:\/\/numa-bord.com\/miniblog\/#website\"},\"datePublished\":\"2017-10-02T07:17:36+00:00\",\"author\":{\"@id\":\"https:\/\/numa-bord.com\/miniblog\/#\/schema\/person\/f9d00acd1703f17e5a6895283eb46a7e\"},\"breadcrumb\":{\"@id\":\"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/#breadcrumb\"},\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/numa-bord.com\/miniblog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"jQuery : soumettre un formulaire contenant des fichiers gr\u00e2ce \u00e0 FormData\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/#website\",\"url\":\"https:\/\/numa-bord.com\/miniblog\/\",\"name\":\"Pense b\u00eate d&#039;un d\u00e9veloppeur web\",\"description\":\"(php, javascript, Symfony, Wordpress....)\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/numa-bord.com\/miniblog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"fr-FR\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/numa-bord.com\/miniblog\/#\/schema\/person\/f9d00acd1703f17e5a6895283eb46a7e\",\"name\":\"Numa\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/f21d1af4658a7106211915940584534c1e0b3eef3f12eb67a697686cad70b64a?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f21d1af4658a7106211915940584534c1e0b3eef3f12eb67a697686cad70b64a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f21d1af4658a7106211915940584534c1e0b3eef3f12eb67a697686cad70b64a?s=96&d=mm&r=g\",\"caption\":\"Numa\"},\"url\":\"https:\/\/numa-bord.com\/miniblog\/author\/negonner\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"jQuery : soumettre un formulaire contenant des fichiers gr\u00e2ce \u00e0 FormData - Pense b\u00eate d&#039;un d\u00e9veloppeur web","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/","og_locale":"fr_FR","og_type":"article","og_title":"jQuery : soumettre un formulaire contenant des fichiers gr\u00e2ce \u00e0 FormData - Pense b\u00eate d&#039;un d\u00e9veloppeur web","og_description":"Aujourd&rsquo;hui il est possible d&rsquo;uploader des fichiers en ajax gr\u00e2ce \u00e0 XMLHttpRequest Niveau 2 et l&rsquo;interface FormData de javascript. Pour l&rsquo;utiliser avec la m\u00e9thode ajax de jQuery il suffit de lui passer comme donn\u00e9es l&rsquo;objet FormData et de configurer les options correctement comme ci-dessous. &nbsp; &nbsp; \/\/au clic du bouton submit des formulaires ayant la [&hellip;]","og_url":"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/","og_site_name":"Pense b\u00eate d&#039;un d\u00e9veloppeur web","article_published_time":"2017-10-02T07:17:36+00:00","author":"Numa","twitter_card":"summary_large_image","twitter_misc":{"\u00c9crit par":"Numa","Dur\u00e9e de lecture estim\u00e9e":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/#article","isPartOf":{"@id":"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/"},"author":{"name":"Numa","@id":"https:\/\/numa-bord.com\/miniblog\/#\/schema\/person\/f9d00acd1703f17e5a6895283eb46a7e"},"headline":"jQuery : soumettre un formulaire contenant des fichiers gr\u00e2ce \u00e0 FormData","datePublished":"2017-10-02T07:17:36+00:00","mainEntityOfPage":{"@id":"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/"},"wordCount":193,"commentCount":0,"articleSection":["D\u00e9veloppement","Javascript","jQuery"],"inLanguage":"fr-FR","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/","url":"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/","name":"jQuery : soumettre un formulaire contenant des fichiers gr\u00e2ce \u00e0 FormData - Pense b\u00eate d&#039;un d\u00e9veloppeur web","isPartOf":{"@id":"https:\/\/numa-bord.com\/miniblog\/#website"},"datePublished":"2017-10-02T07:17:36+00:00","author":{"@id":"https:\/\/numa-bord.com\/miniblog\/#\/schema\/person\/f9d00acd1703f17e5a6895283eb46a7e"},"breadcrumb":{"@id":"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/#breadcrumb"},"inLanguage":"fr-FR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/numa-bord.com\/miniblog\/jquery-soumettre-formulaire-contenant-fichiers-grace-a-formdata\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/numa-bord.com\/miniblog\/"},{"@type":"ListItem","position":2,"name":"jQuery : soumettre un formulaire contenant des fichiers gr\u00e2ce \u00e0 FormData"}]},{"@type":"WebSite","@id":"https:\/\/numa-bord.com\/miniblog\/#website","url":"https:\/\/numa-bord.com\/miniblog\/","name":"Pense b\u00eate d&#039;un d\u00e9veloppeur web","description":"(php, javascript, Symfony, Wordpress....)","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/numa-bord.com\/miniblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"fr-FR"},{"@type":"Person","@id":"https:\/\/numa-bord.com\/miniblog\/#\/schema\/person\/f9d00acd1703f17e5a6895283eb46a7e","name":"Numa","image":{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/secure.gravatar.com\/avatar\/f21d1af4658a7106211915940584534c1e0b3eef3f12eb67a697686cad70b64a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f21d1af4658a7106211915940584534c1e0b3eef3f12eb67a697686cad70b64a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f21d1af4658a7106211915940584534c1e0b3eef3f12eb67a697686cad70b64a?s=96&d=mm&r=g","caption":"Numa"},"url":"https:\/\/numa-bord.com\/miniblog\/author\/negonner\/"}]}},"_links":{"self":[{"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/posts\/140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/comments?post=140"}],"version-history":[{"count":3,"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/posts\/140\/revisions"}],"predecessor-version":[{"id":168,"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/posts\/140\/revisions\/168"}],"wp:attachment":[{"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/media?parent=140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/categories?post=140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/numa-bord.com\/miniblog\/wp-json\/wp\/v2\/tags?post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}