无法在Next.js中使用服务器操作将输入和附加数据组合成formdata

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

Unable to combine input and additional data to formdata in Next.js using server actions

问题

我正在使用Next.js构建一个用于显示我的城市FIFA排名的Web应用程序。我遇到了一个问题,我不知道如何将附加数据包含到FormData中,这些数据不是简单输入的一部分。更准确地说,我正在使用uploadthing,我上传图像,然后在服务器上接收到该图像的链接。

'use client'
import { Input } from "@/components/ui/input"
import { CldUploadButton } from 'next-cloudinary';
import addUser from "@/components/actions/addUser";
import "@uploadthing/react/styles.css";
import { UploadButton } from "@/utils/uploadthing";

export default function Page(){
    return(
        <form className="flex flex-col items-center" action={addUser}>
            <div className='flex flex-col justify-center align-middle gap-4'>
                    <Input className="w-[200px]" type="text"  name="name" placeholder="name" />
                    <Input className="w-[200px]" type="text" name="login" placeholder="login" required />
                    <Input className="w-[200px]" type="text" name="elo" defaultValue={1000} />
            </div>
            <UploadButton
                endpoint="imageUploader"
                onClientUploadComplete={(res) => {
                // Do something with the response
                return res!==undefined ? res[0].fileUrl : null;
                }}
                onUploadError={(error: Error) => {
                // Do something with the error.
                alert(`ERROR! ${error.message}`);
                }}
                />
            <button className="" type="submit">Submit </button>
        </form>
    )
}

我应该怎么做,以便我可以将urllink包含到FormData中?

英文:

I'm working with Next.js and building a web application for FIFA ranking in my city. I have faced the problem, I don't know how to include additional data into FormData that is not a part of simple input. To be more precise, I'm using uploadthing, where I upload image and then receive link of this image on the server.

&#39;use client&#39;
import { Input } from &quot;@/components/ui/input&quot;
import { CldUploadButton } from &#39;next-cloudinary&#39;;
import addUser from &quot;@/components/actions/addUser&quot;;
import &quot;@uploadthing/react/styles.css&quot;;
import { UploadButton } from &quot;@/utils/uploadthing&quot;;

export default function Page(){
    return(
        &lt;form className=&quot;flex flex-col items-center&quot; action={addUser}&gt;
            &lt;div className=&#39;flex flex-col justify-center align-middle gap-4&#39;&gt;
                    &lt;Input className=&quot;w-[200px]&quot; type=&quot;text&quot;  name=&quot;name&quot; placeholder=&quot;name&quot; /&gt;
                    &lt;Input className=&quot;w-[200px]&quot; type=&quot;text&quot; name=&quot;login&quot; placeholder=&quot;login&quot; required /&gt;
                    &lt;Input className=&quot;w-[200px]&quot; type=&quot;text&quot; name=&quot;elo&quot; defaultValue={1000} /&gt;
            &lt;/div&gt;
            &lt;UploadButton
                endpoint=&quot;imageUploader&quot;
                onClientUploadComplete={(res) =&gt; {
                // Do something with the response
                return res!==undefined ? res[0].fileUrl : null;
                }}
                onUploadError={(error: Error) =&gt; {
                // Do something with the error.
                alert(`ERROR! ${error.message}`);
                }}
                /&gt;
            &lt;button className=&quot;&quot; type=&quot;submit&quot;&gt;Submit &lt;/button&gt;
        &lt;/form&gt;
    )
}

What should I do, so I can include urllink into formdata?

答案1

得分: 1

使用 UploadThing API 中的 uploadFiles 而不是提供的 UploadButton,我成功解决了相同的问题。这样,您可以在表单中使用普通的 &lt;input type=&quot;file&quot; /&gt;,并将所选文件添加到服务器操作中的 formData 中。
显然,这意味着如果您希望它执行与 UploadButton 相同的操作,您必须在表单中实现和样式化自己的按钮。

英文:

Had the same question, which I managed to solve by using uploadFiles from the UploadThing API rather than the provided UploadButton. That way, you're using the normal &lt;input type=&quot;file&quot; /&gt; in your form and the chosen file(s) get added to the formData in your server action.
Obviously, this means you have to implement and style your own button in the form if you want it to do the same thing as the UploadButton.

huangapple
  • 本文由 发表于 2023年8月4日 20:43:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836035.html
匿名

发表评论

匿名网友

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

确定