我一直在尝试在我的网站上实现一个S3 bucket,将上传的图像存储到亚马逊的云中。 为了做到这一点,需要一些HTML模板。 我制作了一个HTML文件并复制了锅炉板,现在想在我的。js文件上使用这个文件,其中包含我想显示这些信息的网页。
以下是HTML文件:
<html>
<body>
<h1>Edit your account</h1>
<hr>
<h2>Your avatar</h2>
<input type="file" id="file-input">
<p id="status">Please select a file</p>
<img style="border:1px solid gray;width:300px;" id="preview" src="/images/default.png">
<h2>Your information</h2>
<form method="POST" action="/save-details">
<input type="hidden" id="avatar-url" name="avatar-url" value="/images/default.png">
<input type="text" name="username" placeholder="Username"><br>
<input type="text" name="full-name" placeholder="Full name"><br><br>
<hr>
<h2>Save changes</h2>
<input type="submit" value="Update profile">
</form>
<script>
/*
Function to carry out the actual PUT request to S3 using the signed request from the app.
*/
function uploadFile(file, signedRequest, url){
const xhr = new XMLHttpRequest();
xhr.open('PUT', signedRequest);
xhr.onreadystatechange = () => {
if(xhr.readyState === 4){
if(xhr.status === 200){
document.getElementById('preview').src = url;
document.getElementById('avatar-url').value = url;
}
else{
alert('Could not upload file.');
}
}
};
xhr.send(file);
}
/*
Function to get the temporary signed request from the app.
If request successful, continue to upload the file using this signed
request.
*/
function getSignedRequest(file){
const xhr = new XMLHttpRequest();
xhr.open('GET', `/sign-s3?file-name=${file.name}&file-type=${file.type}`);
xhr.onreadystatechange = () => {
if(xhr.readyState === 4){
if(xhr.status === 200){
const response = JSON.parse(xhr.responseText);
uploadFile(file, response.signedRequest, response.url);
}
else{
alert('Could not get signed URL.');
}
}
};
xhr.send();
}
/*
Function called when file input updated. If there is a file selected, then
start upload procedure by asking for a signed request from the app.
*/
function initUpload(){
const files = document.getElementById('file-input').files;
const file = files[0];
if(file == null){
return alert('No file selected.');
}
getSignedRequest(file);
}
/*
Bind listeners when the page loads.
*/
(() => {
document.getElementById('file-input').onchange = initUpload;
})();
</script>
<div id="root"></div>
</body>
下面是。js文件:
import React, { useState } from "react";
import { Typography, Button, Form, message, Input, Icon } from "antd";
import FileUpload from "../../utils/FileUpload";
import FileUploadNew from "../../utils/fileUploadNew";
import ReactDOM from 'react-dom'
import Axios from "axios";
const { Title } = Typography;
const { TextArea } = Input;
const num = 20
const Continents = [
{ key: 1, value: "MLA" },
{ key: 2, value: "APA" },
];
function UploadProductPage(props) {
const [TitleValue, setTitleValue] = useState("");
const [SubjectValue, setSubjectValue] = useState("");
const [DescriptionValue, setDescriptionValue] = useState("");
const [PriceValue, setPriceValue] = useState(0);
const [ContinentValue, setContinentValue] = useState(1);
const [imageData, setImageData] = useState("");
const [Images, setImages] = useState("");
const onTitleChange = (event) => {
setTitleValue(event.currentTarget.value);
};
const onSubjectChange = (event) => {
setSubjectValue(event.currentTarget.value);
};
const onDescriptionChange = (event) => {
setDescriptionValue(event.currentTarget.value);
};
const onPriceChange = (event) => {
setPriceValue(event.currentTarget.value);
};
const onContinentsSelectChange = (event) => {
setContinentValue(event.currentTarget.value);
};
const updateImages = (newImages) => {
setImages(newImages);
};
const onSubmit = (event) => {
event.preventDefault();
if (
!TitleValue ||
!SubjectValue ||
!DescriptionValue ||
!PriceValue ||
!ContinentValue ||
!Images
) {
return alert("fill all the fields first!");
}
const variables = {
writer: props.user.userData._id,
title: TitleValue,
subject: SubjectValue,
description: DescriptionValue,
price: PriceValue,
images: Images,
continents: ContinentValue,
};
Axios.post("/api/product/uploadProduct", variables).then((response) => {
if (response.data.success) {
alert("Product Successfully Uploaded");
} else {
alert("Failed to upload Product");
}
});
};
const picData = (value) => {
setImages(value);
};
return (
<div style={{ maxWidth: "700px", margin: "2rem auto" }}>
<div style={{ textAlign: "center", marginBottom: "2rem" }}>
<Title level={2}> Upload Prompt</Title>
</div>
<Form onSubmit={onSubmit}>
{/* DropZone */}
{/* <FileUpload refreshFunction={updateImages} /> */}
{/* kamran's code */}
<FileUpload imagePath={picData} refreshFunction={updateImages} />
<br />
<br />
<label>Title</label>
<Input onChange={onTitleChange} value={TitleValue} />
<br />
<br />
<label>Subject</label>
<Input onChange={onSubjectChange} value={SubjectValue} />
<br />
<br />
<label>Description</label>
<TextArea onChange={onDescriptionChange} value={DescriptionValue} />
<br />
<br />
<label>Number of pages</label>
<Input onChange={onPriceChange} value={PriceValue} type="number" />
<br />
<br />
<label>Format</label>
<br>
</br>
<select onChange={onContinentsSelectChange} value={ContinentValue}>
{Continents.map((item) => (
<option key={item.key} value={item.key}>
{item.value}{" "}
</option>
))}
</select>
<br />
<br />
<Button onClick={onSubmit}>Submit</Button>
</Form>
</div>
);
}
export default UploadProductPage;
我一直在阅读并尝试使用getdocumentbyID,但它不起作用。 没有显示错误。 如有任何帮助,不胜感激
React使绑定函数变得很容易,而不必先选择,然后将函数绑定到DOM。
将函数置于返回语句之上
const picData = (value) => {
setImages(value);
};
const uploadFile = (file, signedRequest, url) => {
const xhr = new XMLHttpRequest();
xhr.open('PUT', signedRequest);
xhr.onreadystatechange = () => {
if(xhr.readyState === 4){
if(xhr.status === 200){
document.getElementById('preview').src = url;
document.getElementById('avatar-url').value = url;
}
else{
alert('Could not upload file.');
}
}
};
xhr.send(file);
}
const getSignedRequest = (file) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', `/sign-s3?file-name=${file.name}&file-type=${file.type}`);
xhr.onreadystatechange = () => {
if(xhr.readyState === 4){
if(xhr.status === 200){
const response = JSON.parse(xhr.responseText);
uploadFile(file, response.signedRequest, response.url);
}
else{
alert('Could not get signed URL.');
}
}
};
xhr.send();
}
const initUpload = () => {
const files = document.getElementById('file-input').files;
const file = files[0];
if(file == null){
return alert('No file selected.');
}
getSignedRequest(file);
}
return (
...rest of code here
在代码的DOM部分,将onChange添加到输入
<input type="file" id="file-input" onChange={initUpload}>
通过在组件中使用DangerouslySetInnerHTML
作为道具,可以强制React呈现html。
下面是一个示例实现:
hello.html
<h1>Hello world</h1>
在app.js
中
import React from "react";
import "./styles.css";
import hello from "./hello.html";
export default function App() {
return (
<div className="App">
<div dangerouslySetInnerHTML={{ __html: hello }} />
</div>
);
在您的示例中,使用import htmlFile from'./htmfile.html'
导入html文件,就像普通模块一样,并使用DangerouslySetInerHTML
将其写入div
标记中
强制React在组件中呈现本机。html文件是危险的,这是有原因的。 您可能希望访问此链接以了解更多有关div属性和XSS的信息。