通过HTTP请求的动态网址-Dropzone.js [英] Dynamic url via http request - Dropzone.js

查看:96
本文介绍了通过HTTP请求的动态网址-Dropzone.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:通过dropzone.js上传文件后出现此错误

Problem: I am getting this error after uploading a file via dropzone.js

POST http://localhost:8080/[object%20Promise] 404(不找到)

POST http://localhost:8080/[object%20Promise] 404 (Not Found)

我正在使用dropzone.js库来在应用程序上上传视频. 每个文件的URL每次都是不同.因此,我使用axios发出了http请求,以便每次获取该URL.

I am using dropzone.js library in order to upload videos on an app. The url is different each time for every file. So I make http request - using axios - in order to get each time the url.

我发现的唯一可能的解决方案是将回调函数传递给url(Dropzone.options):

The only possible solution I found was by passing a callback function to the url (Dropzone.options):

   var myDropzone = new Dropzone(".my-upload-container", { 
            url: axios.post('/createURL)
                   .then( function (response) {
                       var theURL = bla
                       return theURL
                   })
          });

当我上传文件时,Dropzone发送POST请求时,出现此404错误.

When I am uploading a file and the Dropzone is sending a POST request, I am getting this 404 error.

问题: 如何获得所需的正确网址?

Question: How can I get the correct url that I want?

我想要的网址示例如下: http://blabla. com/v1/key = bla& token = bla

A sample of a url that I want is like: http://blabla.com/v1/key=bla&token=bla

推荐答案

以下是同一问题的答案: https://stackoverflow .com/a/49900990/3236706

Here's an answer for the same question: https://stackoverflow.com/a/49900990/3236706

Dropzone.autoDiscover = false;

const doStuffAsync = (file, done) => {
  fetch('https://httpbin.org/get').then((response) => {
    file.dynamicUploadUrl = `https://This-URL-will-be-different-for-every-file${Math.random()}`
    done();//call the dropzone done
  })  
}

const getMeSomeUrl = (files) => {
  return `${files[0].dynamicUploadUrl}?sugar&spice`;
}

let myDropzone = new Dropzone("#my-awesome-dropzone", {
  method: "put",
  accept: doStuffAsync,
  url: getMeSomeUrl
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.4.0/min/dropzone.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.4.0/min/dropzone.min.css">

<form action="/file-upload" class="dropzone" id="my-awesome-dropzone">
</form>

这篇关于通过HTTP请求的动态网址-Dropzone.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆