When creating a FormData object from an existing form, are filenames automatically appended and accessible?

坚强是说给别人听的谎言 提交于 2019-12-20 04:15:27

问题


Form

<form id="my_form">
<input type="file" name="my_file">
<input type="text" name="field_one">
<input type="text" name="field_two">
<button>send</button>
</form>

Create FormData Object

var myFormData = new FormData($("#my_form")[0]);

Question

Is the filename of my_file accessible even though it hasn't been specifically defined (for DOM manipulation and inserting into database)?

This states:

You can also append a File or Blob directly to the FormData object, like this:

data.append("myfile", myBlob, "filename.txt");

But it doesn't specify whether a filename is automatically added when creating the FormData object from an existing form.

If it is not automatically appended, is the only option to manually create a FormData object through multiple append() statements in which case filename definition is possible?


回答1:


It seems the filename is automatically added (just tested in Chrome and not sure if this differs in other circumstances).

Steps To Reproduce

  • Go to http://jsfiddle.net/rwone/vsRSf/
  • Open Network tab in Developer Tools
  • Select two images and click the button
  • View the Network tab and you will see the filenames are defined

The image information is only defined with two parameters, and not the third filename parameter:

myFormData.append(name, file);

Original fiddle based on this post:

https://stackoverflow.com/a/21901886/1063287



来源:https://stackoverflow.com/questions/21888560/when-creating-a-formdata-object-from-an-existing-form-are-filenames-automatical

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!