【问题标题】:how to read xml from body nextJS api route如何从正文 nextJS api 路由中读取 xml
【发布时间】:2022-10-24 01:39:42
【问题描述】:

我正在使用 PrimeReact 8.5 fileUpload 和 NextJS 12.3 如何仅获取从 PrimeRact 发送的 XML?

我上传文件

<FileUpload
            name="edcStock"
            url="/api/admin/edcupload"
            onUpload={onUpload}
            accept="text/xml"
            maxFileSize={1000000}
/>

在 nextjs API 我得到文件体

export default function handler(req:      NextApiRequest, res: NextApiResponse) {
const { body } = req;
console.log(body);
return res.status(200).json({ message: 'called api' });
 }

尸体被报道为

------WebKitFormBoundarysugaJxeQVSrDx1AH
Content-Disposition: form-data;    name="edcStock"; filename="edc_xml_en_test.xml"
Content-Type: text/xml

<?xml version="1.0" encoding="UTF-8"?>
<products>
 <product>
   <id>32</id>

【问题讨论】:

    标签: next.js primereact


    【解决方案1】:

    FileUpload 组件发送的 XML 文件作为表单数据发送,您需要在 API 路由中对其进行解析。解析完正文后,您可以从中读取数据并将 XML 转换为 JavaScript 对象。

    这是一个使用formidable 进行表单数据解析,使用fast-xml-parser 将XML 转换为JSON 的示例。

    import fs from 'fs';
    import formidable from 'formidable';
    import { XMLParser } from 'fast-xml-parser';
    
    const parser = new XMLParser();
    
    // Disable built-in parser so we can handle the body ourselves
    export const config = {
        api: {
            bodyParser: false
        }
    };
    
    const handler = async (req, res) => {
        const form = new formidable.IncomingForm();
    
        try {
            // Promisify form parsing so the API route can wait for the parsing to finish
            await new Promise(function (resolve, reject) {
                // Parse form data send in `req.body`
                form.parse(req, async (err, fields, files) => {
                    if (err) {
                        reject(err);
                        return;
                    }
    
                    // Read parsed data. `edcStock` is the `name` given to the uploaded file
                    fs.readFile(files.edcStock.path, function (err, data) {
                        if (err) {
                            reject(err);
                            return;
                        }
                        
                        // Convert XML data to JavaScript object
                        const xmlAsJSON = parser.parse(data);
                        // Handle XML data as you wish
                        console.dir(xmlAsJSON);
                        resolve(xmlAsJSON);
                    });
                });
            });
    
            return res.status(200).json({ response: 'ok' });
        } catch (err) {
            console.error('Error while parsing form', err);
            return res.status(500).json({ error: err });
        }
    };
    
    export default handler;
    

    【讨论】:

      猜你喜欢
      • 2020-09-17
      • 2022-12-05
      • 2021-08-24
      • 1970-01-01
      • 2023-01-04
      • 2021-01-10
      • 1970-01-01
      • 2021-07-04
      • 2023-02-19
      相关资源
      最近更新 更多