如何提交文件而不将其发布到服务器?

发布于 2024-12-17 07:59:57 字数 473 浏览 5 评论 0原文

我希望我的用户能够将图片上传到我的服务器,并且无需他们切换页面即可在上传完成后将该图像弹回该网站上的 div。

这是“更改您的个人资料图片”页面。我需要取回图片,以便我可以调用 jCrop jquery 库,以便他们可以根据自己的喜好裁剪图片。

这是我到目前为止所做的:

@{
    ViewBag.Title = "Profile Picture";
}

<h1>Please choose your new profile picture.</h1>

<input type="file" name="file" />
<input type="submit" value="OK" />

如果我放入 @using BeginForm ,表单将被发回,并且页面将加载到新的位置。我不想要这种行为。如何在不重新加载的情况下使用 AJAX 执行此操作?

I'd like my users to be able to upload a picture to my server and without having them switch pages have that image bounced back to a div on that site once the upload has finished.

This is for a "Change your profile picture" page. I need to get the picture back so I can invoke the jCrop jquery library on it so they can crop the picture to their liking.

Here's what I have so far:

@{
    ViewBag.Title = "Profile Picture";
}

<h1>Please choose your new profile picture.</h1>

<input type="file" name="file" />
<input type="submit" value="OK" />

If I put in the @using BeginForm the form is posted back and the page loads somewhere new. I don't want this behavior. How do I do this with AJAX without reloading?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七颜 2024-12-24 07:59:57

除非您的浏览器支持文件 API,否则您无法通过 ajax 提交文件。 https://developer.mozilla.org/en/using_files_from_web_applications

您可以创建一个 iframe 并使用提交您的图像。

如何异步上传文件?

You can't submit a file via ajax unless your browser supports the file API. https://developer.mozilla.org/en/using_files_from_web_applications

You can create an iframe and use that to submit your image.

How can I upload files asynchronously?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文