fad*_*dah 8 javascript upload preview reactjs react-dropzone
我需要有关如何制作react-dropzone NPM 包的帮助,以便让上传的文件在图像文件(*.png、*.jpg/jpeg、*.gif 等)以外的文件上显示预览——这些都只生成预览美好的)。
Currently, when I use Dropzone for uploading accompanying files on a web form, if I Upload an Image file (*.png, *.jpg, etc.), the Preview we have set up shows just fine with a small thumbnail of the Uploaded file. (see pic below)

However, if I upload another type of file, say an MS-Outlook *.docx, *.xlsx, or say an Adobe Acrobat *.pdf, it just gives me a blank box with a broken file image and whatever alt="..." text I had put in there, in this case, "Uploaded File Preview." (see pic below)

The code we are using was copied nearly verbatim from the Preview example on the React Dropzon Web Site, so I'm wondering if I missed something here?
Here's what I've tried —
react-dropzone <Dropzone> page, with have it set to accept any file type, and giving it the ability to do Previews with the code from https://react-dropzone.js.org/#previews, having the "Preview" <aside> at the bottom of the <Dropzone> section of the code.<Dropzone>.alt="..." text you have in there to describe the file).Dropzone gets imported at the top of the file—
import React, { Component, /* useCallback */ } from "react";
...
import Dropzone from "react-dropzone";
...
Run Code Online (Sandbox Code Playgroud)
Styling/CSS Code for Thumbnails —
...
const thumbsContainer = {
display: "flex",
flexDirection: "row",
flexWrap: "wrap",
marginTop: 16
};
const thumb = {
display: "inline-flex",
borderRadius: 2,
border: "1px solid #eaeaea",
marginBottom: 8,
marginRight: 8,
width: 100,
height: 100,
padding: 4,
boxSizing: "border-box"
};
const thumbInner = {
display: "flex",
minWidth: 0,
overflow: "hidden"
};
const img = {
display: "block",
width: "auto",
height: "100%"
};
...
Run Code Online (Sandbox Code Playgroud)
The onDrop() callback function—
...
onDrop = (acceptedFiles, rejectedFiles) => {
let files = acceptedFiles.map(async file => {
let data = new FormData();
data.append("file", file);
let item = await axios
.post("form/upload", data, {
headers: {
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "application/x-www-form-urlencoded"
}
})
.then(response => {
return Object.assign(file, {
preview: URL.createObjectURL(file),
filename: response.data.filename
});
})
.catch(err => {
let rejects = rejectedFiles.map(async file => {
let data = new FormData();
await data.append("file", file);
console.log("There was an error while attempting to add your files:", err);
console.log("The files that were rejected were:\n", rejects.join('\n'));
})
});
return item;
});
Promise.all(files)
.then(completed => {
let fileNames = completed.map(function(item) {
return item["filename"];
});
this.setState({ files: completed, fileNames: fileNames });
})
.catch(err => {
console.log('DROPZONE ERROR:', err);
});
};
...
Run Code Online (Sandbox Code Playgroud)
And the actual JSX code using <Dropzone> in the React.JS return()—
...
<Form.Field>
<label>Upload Files or Screenshots</label>
<Dropzone accept={acceptedFileTypes} onDrop={this.onDrop}>
{({ getRootProps, getInputProps, isDragActive }) => {
return (
<div
{...getRootProps()}
className={classNames("dropzone", {
"dropzone--isActive": isDragActive
})}
>
<input {...getInputProps()} />
{isDragActive ? (
<div>
<div className="centered">
<Icon name="cloud upload" size="big" />
</div>
<div className="centered">Drop Files Here.</div>
<div className="centered">
<Button className="drop-button">
Or Click to Select
</Button>
</div>
</div>
) : (
<div>
<div className="centered">
<Icon name="cloud upload" size="big" />
</div>
<div className="centered">
Drag and Drop Supporting Files here to Upload.
</div>
<div className="centered">
<Button className="drop-button">
Or Click to Select
</Button>
</div>
</div>
)}
</div>
);
}}
</Dropzone>
<aside style={thumbsContainer}>{thumbs}</aside>
</Form.Field>
...
Run Code Online (Sandbox Code Playgroud)
Expected behavior —
I would prefer all file types to generate a correct preview.
My Set-up —
... So, am I doing something wrong so that previews on other files besides Images aren't working? Is this not a feature for anything but Image files? Please advise.
A couple other questions —
react-dropzone, is there a way to have Image Files generate Previews, but all other files just list the File name being uploaded and such, as in the some of the other examples on your React Dropzone Web Site? If so, how do you switch between the two to have both as you drag & drop files onto the <Dropzone>?<Dropzone>, and then attempt to drag and drop a second time, that wipes the first set of files from being uploaded and replaces them with the new files just dropped on the <Dropzone>. Is there a way to have the <Dropzone> cumulatively add files on each time they are dropped there, not just wipe out the previous files and replace with the new? If so, what are the steps to do that, please?I do appreciate any constructive reply, please. Thank you in advance.
只是为了澄清一下,React-dropzone 不是在页面上显示预览的那个。您正在使用图像标签来做到这一点。
现在,如果您要求显示所有文档文件类型的预览,您首先需要一个所需类型的解析器,因为浏览器不支持读取 pdf 以外的文档(.doc/.xlsx)。像react-doc-viewer
之类的东西。
这样您就可以读取上传的文件并使用包渲染器来显示文件
const docs = [
{ uri: "https://url-to-my-pdf.pdf" },
{ uri: require("./example-files/pdf.pdf") }, // Local File
];
...
<DocViewer documents={docs} />;
Run Code Online (Sandbox Code Playgroud)
const thumbs = files.map(file => {
// Add other images types as needed
if(["image/png", "image/jpeg"].includes(file.type)){
return (
<div style={thumb} key={file.name}>
<div style={thumbInner}>
<img
src={file.preview}
style={img}
// Revoke data uri after image is loaded
onLoad={() => { URL.revokeObjectURL(file.preview) }}
/>
</div>
</div>
);
}
// Handling other file types
return (
<div style={thumb} key={file.name}>
<div style={thumbInner}>
<p>{file.name}</p
</div>
</div>
);
});
Run Code Online (Sandbox Code Playgroud)
您可以为不同的文件 MIME 类型添加不同的条件。文件 mime 类型的完整列表可以在mdn 文档
中找到
let fileNames = [...this.state.filenames,
...completed.map(function(item) {
return item["filename"];
})];
this.setState({files: [...this.state.files, ...completed], fileNames: fileNames });
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5810 次 |
| 最近记录: |