我想for in使用Typescript循环遍历 FileList 。我从一个常规的旧 for 循环开始,效果非常好:
let files:FileList = e.target.files
for(let i = 0;i<files.length;i++){
doSomething(files[i])
}
function doSomething(f:File){
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用较短的for..in循环时:
for(let f in files){
doSomething(f)
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:Argument of type 'string' is not assignable to parameter of type 'File'。因此,f尽管 FileList 包含文件而不是字符串,但它在某种程度上被视为字符串。
这些修复不起作用:
for(let f:File in files){ // left hand assignment not allowed
doSomething(f as File) // cannot convert string to File
}
Run Code Online (Sandbox Code Playgroud)
这是不可能的吗?
我使用名为react-dropzone 和React-Redux 的Node 包模块来允许用户将文件拖到窗口上并显示文件信息。
React-dropzone 组件为我提供了标准event.dataTransfer.files中的 FileList 。
我遇到的问题是,当我将该 FileList 分派到 Redux 存储时,File 项尚未加载到 FileList 数组中,因此当我分派 File 数据时,我只是得到{"preview":"blob:file:///f30eafb8-8a77-4402-9111-379590b4a03f"}.
如果我将文件项记录到控制台,它们显示得很好,因为它们的数据在我查看时已加载,但我不知道如何仅在加载后分派文件信息。
下面是当我将 FileList 数组中的一个文件项记录到控制台时所看到的示例。
{
lastModified: 1473709614000,
lastModifiedDate: Mon Sep 12 2016 12:46:54 GMT-0700 (PDT),
name: "test_image.jpg",
path: "/Users/mitchconquer/Desktop/test_image.jpg",
preview: "blob:file:///0fe69686-125d-464a-a0a8-a42e497d3808",
size: 41805,
type: "image/jpeg",
webkitRelativePath: ""
}
Run Code Online (Sandbox Code Playgroud)
我相信问题只是文件对象未完全加载,但我无法找到一种方法来仅在数据完全加载后才触发操作。有人能指出我正确的道路吗?
这是我使用react-dropzone的组件:
// FileDrop.js
import React, { Component, PropTypes } from 'react';
import { Link } from 'react-router';
import styles from './Home.css';
import Dropzone from 'react-dropzone';
export default class FileDrop extends …Run Code Online (Sandbox Code Playgroud) angular.module('myApp')
.directive('fileModel', ['$parse', 'uploadService','messageService','CONF', function ($parse, uploadService,messageService,CONF) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function () {
var file = element[0].files[0];
var fd = new FormData();
fd.append('file', file);
console.log(fd)
self.isDisabled = true;
ParkingService.uploadImage(fd).then(
function (response) {
response = JSON.parse(response);
element[0].files[1] = response;
console.log(response)
if (response.message !== 'ERROR') {
// self.image = response.result;
messageService.toasterMessage(CONF.TOASTER_TOP_RIGHT,CONF.TOASTER_SUCCESS,"Success Upload File");
}
else {
messageService.toasterMessage(CONF.TOASTER_TOP_RIGHT,CONF.TOASTER_ERROR,response.result);
}
}
);
scope.$apply(function () {
modelSetter(scope, element[0].files); …Run Code Online (Sandbox Code Playgroud) 我允许用户上传多个文件。然后我需要迭代这些文件并执行一些操作。我想添加一些额外的功能FileList,但 TS 不知道forEach数组FileList。这是我的代码:
public uploadMultiple(evt){
console.log(evt.files);
FileList.prototype.forEach = function(callback) {[].forEach.call(this, callback)};
}
Run Code Online (Sandbox Code Playgroud) 我有一个包含许多子目录的文件夹,其中包含许多不同类型的文件。我只需要选择具有以下扩展名 *.txt 和 *.shp 的文件。我尝试将模式与 & 和 | 一起使用 组合,但似乎不起作用。| 的 | 运算符仅选择最后写入的文件格式(在下面的代码中,它仅选择 *.shp 文件,而不选择 *.txt)
filelist <- list.files(path = ".",pattern = '*.txt$ | *.shp$', recursive = TRUE,ignore.case = TRUE, include.dirs = TRUE, full.names = TRUE)
我已经用谷歌搜索并搜索了文档。我被困在这个问题上。
你知道subversion 忽略的文件列表位于哪里吗?
我在网上看到了如何加载包含空格但尚未存在Ant任务的文件列表.
我有一个文件,每行包含一个文件路径,如下所示:
dir1/dir2/dir with spaces/file1.js
dir1/dir2/dir with spaces/dir3/file2.js
dir1/file1.js
Run Code Online (Sandbox Code Playgroud)
由于路径有空格我不能使用:
<filelist files="..." />
Run Code Online (Sandbox Code Playgroud)
这些文件还不存在,所以似乎我无法使用
<fileset>
<includesfile name="..." />
</fileset>
Run Code Online (Sandbox Code Playgroud)
任何想法都会受到很大的影响.
我正在使用以下代码将画布图像隐藏到Blob。
为了将Blob转换为文件/文件列表对象,我需要将该文件列表传递给文件处理程序。
我的代码:
var canvas1 = document.getElementById("preview");
var theImage = document.getElementById("blahProfile");
theImage.src = canvas1.toDataURL();
var blob = dataURItoBlob(theImage.src);
Run Code Online (Sandbox Code Playgroud)
有什么办法可以将该Blob转换为文件对象?
我有一个FileList对象,它保存以前上传的文档.我正在尝试使用另一个函数通过使用另一个FileList对象向此集合添加更多文件,因此我需要将辅助FileList对象"追加"到主要对象上.怎么可能实现这个目标?
我有一个包含 1000 多个图像文件的文件列表对象。我希望对象按文件名(字母数字)排序。换句话说,我想做一个自然排序。文件名是这样的:
d_1_ct.png
d_2_ct.png
d_3_ct.png
d_4_ct.png
Run Code Online (Sandbox Code Playgroud)
[].slice.call(filelist_object)通过执行orArray.from(filelist_object)然后调用将文件列表对象转换为数组,sort()结果仅按字母顺序排序。如何使文件列表对象自然地按文件名排序?
只要我能够在数组元素上显示图像文件,我就可以将其转换为URL.createObjectURL()数组。
尽管文件列表对象中的文件名是字母数字字符串,但自然排序的字母数字字符串不是我想要的。文件列表对象包含以下属性:
0: File
lastModified: 1493435514308
lastModifiedDate: Sat Apr 29 2017 08:41:54 GMT+0530 (India Standard Time)
name: "d_1_ct.png"
size: 5307
type: "image/png"
webkitRelativePath: "img/d_1_ct.png"
__proto__:File
Run Code Online (Sandbox Code Playgroud)
我想对name或进行排序webkitRelativePath,同时保留文件列表对象的所有属性,因为我使用对象的索引号来显示图像。
filelist ×10
javascript ×4
typescript ×2
angularjs ×1
ant ×1
blob ×1
ignore ×1
natural-sort ×1
object ×1
r ×1
reactjs ×1
redux ×1
svn ×1