如何在 javascript 中从 input=File 更改文件名 [英] How to change name of file in javascript from input=File

查看:21
本文介绍了如何在 javascript 中从 input=File 更改文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上传到共享点网站时,我需要更改文件名(不是文件,只是名称的元数据).

I need to change the filename (not the file, just the metadata of the name) when uploading to a sharepoint site.

我认为在 javascript 中更改 html 属性很容易,而不是使用 Sharepoint 后端.这样当我上传文件时,它会更改文件的名称(而不是数据)

I figured that it would be easy enough to change the html attribute in javascript rather than playing with Sharepoint backend. So that when I upload a file it changes the name of the file (not the data)

像这样:

function PreSaveAction(){
   var file = document.GetElementById('fileupload1');
   file.files[0].name='ChangedName.tmp'

return true;
}

由于锁定 input='file' 属性的性质,这是不可能的吗?

Is this impossible due to the nature of the locked input='file' attributes?

推荐答案

试试这个:

var element = document.GetElementById('fileupload1');
var file = element.files[0];
var blob = file.slice(0, file.size, 'image/png'); 
newFile = new File([blob], 'name.png', {type: 'image/png'});

注意:这是针对图像类型的,您必须使用实际使用的类型更改此类型.

note: this is for a image type, you have to change this type with type you're actually using.

这篇关于如何在 javascript 中从 input=File 更改文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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