简体   繁体   English

更改客户端上传文件的名称

[英]Change name of uploaded file on client

I have the following. 我有以下内容。

<form method="post" action="/send" enctype="multipart/form-data">
    <input type="file" name="filename" id="AttachFile">
</form>

I want to change the name of the file the user uploads. 我想更改用户上传的文件的名称。

If the user selects "Document.docx" I want to change it to "Bank - Document.docx". 如果用户选择“Document.docx”,我想将其更改为“Bank - Document.docx”。

I still want to read the file the user selected, not some other file, just use a different name for it when sending to the server. 我仍然想要读取用户选择的文件,而不是其他文件,只是在发送到服务器时使用不同的名称。

I'm working within bounds of an application which doesn't allow control of the server side, so ideally I need to do this on the client. 我在一个不允许控制服务器端的应用程序的范围内工作,所以理想情况下我需要在客户端上执行此操作。 Furthermore I need this to work within the confines of a form . 此外,我需要在form的范围内工作。

I have tried variations of the following without success: 我尝试过以下变体而没有成功:

document.getElementById("AttachFile").name = "test.txt"
document.getElementById("AttachFile").files = "test.txt"
document.getElementById("AttachFile").value ="test.txt"

You can do it through the File API . 您可以通过File API We can also use the Blob API to be compatible with Microsoft edge . 我们还可以使用Blob API 与Microsoft edge兼容

var file = document.getElementById("AttachFile").files[0];
var newFile = new File([file], "Bank - Document.docx", {
  type: file.type,
});

Here's a complete example — see comments: 这是一个完整的例子 - 见评论:

HTML: HTML:

<input type="file" id="AttachFile">
<input type="button" id="BtnSend" value="Send">

JavaScript: JavaScript的:

document.getElementById("BtnSend").addEventListener("click", function() {
    // Get the file the user picked
    var files = document.getElementById("AttachFile").files;
    if (!files.length) {
        return;
    }
    var file = files[0];
    // Create a new one with the data but a new name
    var newFile = new File([file], "Bank - Document.docx", {
      type: file.type,
    });
    // Build the FormData to send
    var data = new FormData();
    data.set("AttachFile", newFile);
    // Send it
    fetch("/some/url", {
        method: "POST",
        body: data
    })
    .then(response => {
        if (!response.ok) {
            throw new Error("HTTP error " + response.status);
        }
        return response.text(); // or response.json() or whatever
    })
    .then(response => {
        // Do something with the response
    })
    .catch(error => {
        // Do something with the error
    });
});

You can't rename the file using a standard form submission. 您无法使用标准form提交重命名该文件。 The name of the file being uploaded is read-only. 要上载的文件的name是只读的。 To do this, you'd have to do it server-side. 要做到这一点,你必须在服务器端做。 (The designers of file uploads seem to have either not considered this rename-on-upload use case or not felt it needed to be addressed by the API.) (文件上传的设计者似乎没有考虑过这个重命名的上传用例,或者认为它不需要由API解决。)

However, you can prevent the default form submission and instead submit it programmatically via ajax, which does allow you to rename the file; 但是,您可以防止默认的表单提交,而是通过AJAX编程方式提交,这确实让你重命名文件; see man tou's answer . 看到man tou的回答

If you cannot work on the server side then you have to either rename the file BEFORE upload or AFTER download. 如果您无法在服务器端工作,则必须在上载或下载后重命名该文件。 How you present the name for the user is you to decide. 您可以决定如何为用户显示名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM