.net core 无法将文件从脚本上载到.net Core中的Rest API
大家好,希望大家身体健康 我已经用这些技术编程很短时间了(React hooks和.net Core) 我试图以文件类型输入的形式从前端向后端的api发送一个.txt文件,但根据发送方式,我会收到多个错误。我当前试图通过FormData发送该文件,但它会抛出415媒体类型错误.net core 无法将文件从脚本上载到.net Core中的Rest API,.net-core,file-upload,react-hooks,.net Core,File Upload,React Hooks,大家好,希望大家身体健康 我已经用这些技术编程很短时间了(React hooks和.net Core) 我试图以文件类型输入的形式从前端向后端的api发送一个.txt文件,但根据发送方式,我会收到多个错误。我当前试图通过FormData发送该文件,但它会抛出415媒体类型错误415不支持的媒体类型 我想展示我认为是正确的代码来等待您的帮助< /P>的必要代码。 这是mi API端点: public class FileUploadEndpoint : BaseAsyncEndpoint.With
415不支持的媒体类型
我想展示我认为是正确的代码来等待您的帮助< /P>的必要代码。 这是mi API端点:
public class FileUploadEndpoint : BaseAsyncEndpoint.WithRequest<FormFileDto>.WithResponse<Response<string>>
{
private readonly IMovimientoBusinessLogic _movimeintoBusinessLogic;
public FileUploadEndpoint(IMovimientoBusinessLogic movimientoBusinessLogic)
{
_movimeintoBusinessLogic = movimientoBusinessLogic;
}
[HttpPost("api/fileupload")]
[Consumes("multipart/form-data")]
[ProducesResponseType(typeof(Response<string>), StatusCodes.Status200OK)]
public async override Task<ActionResult<Response<string>>> HandleAsync(FormFileDto request, CancellationToken cancellationToken = default)
{
return new Response<string> { Data = request.formFile};
}
}
}
如果你能帮助我,谢谢你:)
export const FileUpload = (props?: any) => {
const classes = useStyles();
const { Props } = props;
let filereader:any;
const[file,setFile]=useState("");
const handleFileReaded =(e:any)=>{
const content=filereader?.result
setFile(content)
uploadFile(content)
}
const handleFileChosen=(file:any)=>{
filereader=new FileReader();
filereader.onloadend=handleFileReaded;
filereader.readAsText(file,"UTF-8");
}
useEffect(()=>{
console.log(file)
},[file])
const uploadFile= async (e:any)=>{
var prueba:string;
prueba=(JSON.stringify(e)).toString()
var formData=new FormData()
formData.append("content",(JSON.stringify(e)).toString())
await Props.startPostArchivoNomina(Props.loginState.claims, e);
}
return (
<Container>
<Card>
<Typography variant="h6" align="center" className="titulo1">
Cargar Archivo
</Typography>
<Typography variant="h6" align="center" className="titulo2">
Nomina
</Typography>
<Grid container spacing={2} className={classes.btn}>
<Grid item xs={6} md={2}>
<Button
variant="contained"
component="label"
color="primary"
><input type="file" onChange={e=>handleFileChosen(e.target.files!=null?e.target.files[0]:"")} hidden />
CARGAR ARCHIVO
</Button>
</Grid>
<Grid item xs={6} md={2}>
<Button variant="outlined" component="label" color="secondary" >
Aprobar Cargas
</Button>
</Grid>
</Grid>
</Card>
</Container>
);
};
export const ApiAsyncFile = async<T> (req: RequestModel): Promise<Response<T>> => {
return fetch(req.url, {
method: req.method,
headers: {
'Accept': 'aplication/form-data, application/json, application/xml, text/plain, text/html, *.*',
'Content-Type': "aplication/form-data",
'Authorization': `Bearer ${req.token}`,
},
body: JSON.stringify(req.body),
})
.then(response => response.json())
.catch(error => error);
}
public class FormFileDto
{
public string formFile { get; set; }
}