从现有表单创建FormData对象时,文件名是否会自动附加并可以访问? [英] When creating a FormData object from an existing form, are filenames automatically appended and accessible?

查看:310
本文介绍了从现有表单创建FormData对象时,文件名是否会自动附加并可以访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表格

<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>

创建FormData对象

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

问题

my_file的文件名是否可以访问,即使没有特别定义(用于DOM操作和插入数据库)?

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

状态:

您还可以将File或Blob直接附加到FormData对象, 像这样:

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

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

但是,它没有指定从现有表单创建FormData对象时是否自动添加文件名.

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

如果未自动附加,则是通过多个append()语句手动创建FormData对象的唯一选择,在这种情况下可以定义文件名?

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?

推荐答案

似乎文件名已自动添加(刚刚在Chrome中进行了测试,不确定在其他情况下是否有所不同).

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

复制步骤

  • 转到 http://jsfiddle.net/rwone/vsRSf/
  • 在开发人员工具中打开网络"标签
  • 选择两个图像,然后单击按钮
  • 查看网络"标签,您将看到文件名已定义
  • 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

这篇关于从现有表单创建FormData对象时,文件名是否会自动附加并可以访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆