[英]Upload with JavaScript using Firebase Storage and Dropzone
I have been doing a lot of research and have yet to find a viable solution for the following.我一直在做很多研究,但尚未找到以下可行的解决方案。 I asked a more specific question here ) but due to the lack of answers or comments, I decided to ask a more generalized question that may be applicable to more people.
我在这里问了一个更具体的问题)但由于缺乏答案或评论,我决定问一个可能适用于更多人的更笼统的问题。
Anyway, dropzone.js allows you to create a file upload with js.无论如何,dropzone.js 允许您使用 js 创建文件上传。 Below is a snippet of code that does just that:
下面是执行此操作的代码片段:
var myDropzone = new Dropzone(document.body, {
url: "/" // Not sure what to put here...
});
Where the url: "" would be equivalent to the action=""
in a form.其中 url: "" 相当于表单中的
action=""
。 Most examples you can find have url: "upload.php" however I am trying to upload the images to firebase.您可以找到的大多数示例都有 url: "upload.php" 但是我正在尝试将图像上传到 firebase。 Is there a way to run js for the file upload instead of php?
有没有办法为文件上传运行js而不是php? perhaps url: "upload.js"?
也许网址:“upload.js”? That wouldn't work but just to show what I mean.
那行不通,只是为了表明我的意思。 Or is there a way to upload images to Firebase with PHP?
或者有没有办法使用 PHP 将图像上传到 Firebase? I am not quite sure how to proceed.
我不太确定如何进行。
Here is what dropzone.js says about the URL attribute, I just don't understand it fully:下面是 dropzone.js 关于 URL 属性的说明,我只是不完全理解它:
URL: Has to be specified on elements other than form (or when the form doesn't have an action attribute).
URL:必须在表单以外的元素上指定(或者当表单没有 action 属性时)。 You can also provide a function that will be called with files and must return the url (since v3.12.0)
您还可以提供一个将被文件调用且必须返回 url 的函数(自 v3.12.0 起)
You can't upload directly to Firebase Storage via some special URL that you can put in a web form.您不能通过一些可以放在网络表单中的特殊 URL 直接上传到 Firebase 存储。 But you can easily implement an endpoint with Cloud Functions for Firebase with a little extra code you write that runs on the server side.
但是,您可以使用 Cloud Functions for Firebase 轻松实现端点,只需编写一些在服务器端运行的额外代码。
I have a short Gist that gives all the code that makes it happen.我有一个简短的 Gist,其中提供了实现它的所有代码。
You can watch the code in action in this tweet .您可以在此推文中观看正在运行的代码。
I hosted my web site via Firebase Hosting, but if you don't do that, you may need to set up CORS or something to be able to call from your page to Cloud Functions.我通过 Firebase Hosting 托管了我的网站,但如果您不这样做,您可能需要设置 CORS 或其他东西才能从您的页面调用 Cloud Functions。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.