鍍金池/ 問答/HTML5  Linux  HTML/ 如何更改<input type="file" /&gt

如何更改<input type="file" />上傳文件的名字?

在選擇文件后上傳文件前,或者是點(diǎn)擊提交的時(shí)候,如何更改上傳文件的名字?

<form id="upload_form" method="post" enctype="multipart/form-data">
    <input type="file" name="upload_file">
    <input type="submit">
</form>

直接改變file.name是無效的;

var fileExtension = '.' + file.name.split('.').pop();
file.name = Math.random().toString(36).substring(7) + fileExtension;
console.log(file);

應(yīng)該如何更改呢?

回答
編輯回答
我不懂

上傳的時(shí)候用FormData提交

2017年12月9日 21:23
編輯回答
夢一場

使用FormData進(jìn)行提交,修改文件名用以下方法:

formData.append(name, value, filename);

參考地址:https://developer.mozilla.org...

注意:如果是移動(dòng)端,iphone請注意旋轉(zhuǎn)圖片角度。

希望對您有所幫助!

2018年3月17日 16:09
編輯回答
瞄小懶

文件使用formdata提交

2017年7月31日 19:29
編輯回答
何蘇葉
<body>
 
    <form id="form1" enctype="multipart/form-data">
        <input id="image-file" type="file" name="file1" />
        <input id="btn-submit" type="submit" value="submit" />
    </form>
</body>
<script>
    $(document).ready(function() {
 
        initImageUpload();
 
        function initImageUpload() {
            $("#btn-submit").click(function(e) {
                e.preventDefault();
 
                var everlive = new Everlive({
                    appId: "",
                    scheme: "https"
                });
 
                // construct the form data and apply new file name
                var file = $('#image-file').get(0).files[0];
 
                var newFileName = file.filename + "new";
                var formData = new FormData();
                formData.append('file', file, newFileName);
 
 
                $.ajax({
                    url: everlive.files.getUploadUrl(), // get the upload URL for the server
                    success: function(fileData) {
                        alert('Created file with Id: ' + fileData.Result[0].Id); // access the result of the file upload for the created file
                        alert('The created file Uri is available at URL: ' + fileData.Result[0].Uri);
                    },
                    error: function(e) {
                        alert('error ' + e.message);
                    },
                    // Form data
                    data: formData,
                    type: 'POST',
                    cache: false,
                    contentType: false,
                    processData: false
                });
                return false;
            });
        }
    });
</script>
2018年6月14日 01:22