发送音频文件在使用Remix.run(1.16)的multipart/form-data表单POST中。

huangapple go评论68阅读模式
英文:

Send an Audio File in a multipart/form-data Form POST Using Remix.run (1.16)

问题

我试图通过表单的POST请求将音频文件发送到服务器端。

我试图在Remix文档这里中所指示的方法中,将 unstable_parseMultipartFormData 与 multipart/form-data表单结合使用,但我得到了这个错误:TypeError: uploadHandler2 不是一个函数
在 parseMultipartFormData 处发生

我试图在服务器端内使用这个文件。

export const action = async ({ request, context }) => {
   const formData = await unstable_parseMultipartFormData(
      request,
      uploadHandler
   );
   return '''
};

export default function Index() {
   const submit = useSubmit()
   const [formData, setFormData] = useState({
      audioFile: '''',
   });

   const handleFileChange = (event) => {
      setFormData({ audioFile: event.target.files[0] });
   };

   const handleSubmit = async (e) => {
      e.preventDefault();
      submit(formData, { method: "post" });
   };

   return (
      <>
         <section>
            <Form encType="multipart/form-data" method="POST" >
               <input
                  id="fileUpload"
                  name="audioFile"
                  type="file"
                  accept=".mp3,.m4a"
                  // onChange={handleFileChange}
                  className="bg-neutral-600 text-white px-4 py-2 rounded-lg mb-4 w-full text-sm"
               />
               <button className="text-white border border-white p-2 rounded-md text-sm" type="submit">Send</button>
            </Form>

         </section>
      </>
   );
}

帮助将不胜感激。

谢谢。

英文:

I'm trying to send an audio file to the server side through a post request of a form.

I'm trying to use unstable_parseMultipartFormData in conbination with multipart/form-data Form as instructed on Remix documentation here but I get this error: TypeError: uploadHandler2 is not a function
at parseMultipartFormData

Im trying to use this file on server side inside the action.

export const action = async ({ request, context }) => {
   const formData = await unstable_parseMultipartFormData(
      request,
      uploadHandler
   );
   return ''
};

export default function Index() {
   const submit = useSubmit()
   const [formData, setFormData] = useState({
      audioFile: '',
   });

   const handleFileChange = (event) => {
      setFormData({ audioFile: event.target.files[0] });
   };

   const handleSubmit = async (e) => {
      e.preventDefault();
      submit(formData, { method: "post" });
   };

   return (
      <>
         <section>
            <Form encType="multipart/form-data" method="POST" >
               <input
                  id="fileUpload"
                  name="audioFile"
                  type="file"
                  accept=".mp3,.m4a"
                  // onChange={handleFileChange}
                  className="bg-neutral-600 text-white px-4 py-2 rounded-lg mb-4 w-full text-sm"
               />
               <button className="text-white border border-white p-2 rounded-md text-sm" type="submit">Send</button>
            </Form>

         </section>
      </>
   );
}

Help is be greatly appreciated.

Thank you.

答案1

得分: 1

要上传文件,您需要设置<Form encType>

<Form method="post" encType="multipart/form-data">
  <input type="file" name="audioFile" />
  <button>提交</button>
</Form>

然后创建一个带有uploadHandler的动作:

import { 
  unstable_createMemoryUploadHandler,
  unstable_parseMultipartFormData, 
} from '@remix-run/node';

export const action = async ({ request }: ActionArgs) => {
  const uploadHandler = unstable_createMemoryUploadHandler({
    maxPartSize: 500_000,
  });
  const formData = await unstable_parseMultipartFormData(
    request,
    uploadHandler
  );
  // 现在您可以将音频文件作为File对象获取
  const audioFile = formData.get("audioFile") as File;
  // ...
}

详细信息请参阅:https://remix.run/docs/en/1.16.1/utils/parse-multipart-form-data

英文:

To upload files, you need to set the &lt;Form encType&gt;:

&lt;Form method=&quot;post&quot; encType=&quot;multipart/form-data&quot;&gt;
  &lt;input type=&quot;file&quot; name=&quot;audioFile&quot; /&gt;
  &lt;button&gt;Submit&lt;/button&gt;
&lt;/Form&gt;

Then create an action with an uploadHandler:

import { 
  unstable_createMemoryUploadHandler,
  unstable_parseMultipartFormData, 
} from &#39;@remix-run/node&#39;;

export const action = async ({ request }: ActionArgs) =&gt; {
  const uploadHandler = unstable_createMemoryUploadHandler({
    maxPartSize: 500_000,
  });
  const formData = await unstable_parseMultipartFormData(
    request,
    uploadHandler
  );
  // now you can get the audio file as a File object
  const audioFile = formData.get(&quot;audioFile&quot;) as File;
  ...
}

https://remix.run/docs/en/1.16.1/utils/parse-multipart-form-data

huangapple
  • 本文由 发表于 2023年6月6日 06:55:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定