我想将示例 xlsx 文件放在我的公共文件夹(react 项目)中,其他人可以下载该文件。
<Button><link rel="chargeSample" href="%PUBLIC_URL%/chargeSample.xlsx"></link></Button>
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我使用msw来模拟自动化 FE 测试的后端响应。
我想为用户创建的数据创建导出功能。我正在POST向所需的路线发出请求,并希望返回带有 .csv 的响应;.pdf ;.xls 文件应自动下载。
我一直在寻找合适的方法来做到这一点,并遇到了Content-Disposition响应标头。
遗憾的是,响应没有触发保存对话框,也没有开始下载,但我找不到错误。
编辑:经过几次对话和一些独特的情况后,我决定为生成的 Blob 数据创建一个 ObjectURL,并通过调用强制下载
window.open()。我更改了处理程序中的预期响应,并在 FE 中添加了相应的调用。
处理程序.js
Run Code Online (Sandbox Code Playgroud)rest.post('/mock/things/:thingId/export', async (req, res, ctx) => { const docType = req.headers.get('Content-type'); const startDate = req.url.searchParams.get('startDate'); const endDate = req.url.searchParams.get('endDate'); const selected = req.body; const resDataString = 'testName,testLastName,\r\nDaniel,Schaller'; const blob = new Blob([resDataString], { type: docType }); const buffer = await blob.arrayBuffer(); return res( ctx.status(200), ctx.set({ 'content-type': `${docType}`, 'Content-length': buffer.byteLength.toString(), }), ctx.body(buffer), ctx.delay(), …
I created an API with Laravel. I use Vuejs for the front end.
I would like to generate a PDF based on the user and download it with view. Only, it does not work.
Here is my controller:
public function generate($id)
{
$data = CoverLetter::where([
['id', '=', $id],
['user_id', '=', auth()->user()->id]
])->first();
$path = public_path() . '/pdf/' . $data->filename . '.pdf';
$pdf = PDF::loadView('pdf.coverletter', $data);
$pdf->save($path);
return response()->download($path);
}
Run Code Online (Sandbox Code Playgroud)
And my function in vue :
async downloadCoverLetter(file) {
axios.get('/cover-letter/generate/' + …Run Code Online (Sandbox Code Playgroud) 我在前端使用 React 和 Graphql,在后端使用 Django 和 Graphene。
我希望能够下载报告的 pdf 文件。我尝试使用突变来做到这一点,如下所示:
const [createPdf, {loading: createPdfLoading, error: createPdfError}] = useMutation(CREATE_PDF)
const handleCreatePDF = async (reportId) => {
const res = await createPdf({variables: {reportId: parseInt(reportId) }})
debugger;
};
export const CREATE_PDF = gql`
mutation ($reportId: Int!) {
createPdf (reportId: $reportId){
reportId
}
}
`;
Run Code Online (Sandbox Code Playgroud)
在后端,我有这样的事情:
class CreatePDFFromReport(graphene.Mutation):
report_id = graphene.Int()
class Arguments:
report_id = graphene.Int(required=True)
def mutate(self, info, report_id):
user = info.context.user
if user.is_anonymous:
raise GraphQLError("You are not logged in!")
report …Run Code Online (Sandbox Code Playgroud)