上传 - 重新发送表格 - 附加文件 - Symfony
我有 Symfony 1.4 表单:
$this->setWidget('title', new sfWidgetFormInputText());
$this->setWidget('photo', new sfWidgetFormInputFile());
如果我发送此表单,并且他有效,则显示有错误的页面。在输入标题中是我输入的先前文本,但没有附加文件的链接。我必须再次搜索该文件并选择。我该如何修复它?
I have in Symfony 1.4 form:
$this->setWidget('title', new sfWidgetFormInputText());
$this->setWidget('photo', new sfWidgetFormInputFile());
if i send this form, and he is valid then show me page with error. In input TITLE is previous text, which i entered, but no link to attach files. I must again search this file and select. How can i fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简而言之,你不能。这是 HTML 文件输入的症状。
文件输入采用客户端计算机(而不是服务器)上文件的本地路径,因此请考虑以下(假设的)场景:
这也将是一个重大的安全风险。假设您编写了一个包含表单和文件输入的恶意脚本。如果您知道客户端计算机上文件的路径,那么您可以预先填充文件输入,然后使用 JavaScript 提交表单以强制用户上传文件。
这就是为什么您无法在 HTML 中预填充文件输入的原因。
作为替代方案,您应该使用 sfWidgetFormInputFileEditable 它将在文件输入旁边显示先前上传的图像。但是,只有在表单成功提交并保存后才会显示。
In short, you can't. This is a symptom of HTML file inputs.
A file input takes a the local path of the file on the client's machine (not the server), so consider this (hypothetical) scenario:
It would be a major security risk also. Let's say you write a malicious script which contains a form and file input. If you knew the path to a file on the client's machine then you could pre-populate the file input and then submit the form with JavaScript to force the user to upload the file.
This is why you can't pre-populate a file input in HTML.
As an alternative you should use
sfWidgetFormInputFileEditable
which will display the previously uploaded image alongside the file input. However, this will only display once the form has submitted and saved successfully.