社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Jquery

如何通过web api上传a文件(图片)并用jquery更新数据?

Dave • 7 年前 • 2295 次点击  

我正在尝试通过web api将一个图像作为化身照片上传到服务器。

任何人都可以解释如何通过当前用户id has token将图像url插入到数据中?

我已经做了 $.getJSON (url,... 我可以在任何地方阅读和附加它们。

我有这样的东西:

用户ID=1

/API/图像/上传表单/化身

/API/用户/更新用户信息

一。如何使用jquery附加图像文件并通过web api发布?

2.我怎样才能重新上传(更新)文件?

 $(document).ready(function (e) {
     $('#upload').on('click', function () {
     var file_data = $('#file').prop('files')[0];
     var form_data = new FormData();
     form_data.append('file', file_data);
     $.ajax({
             url: 'https://myurl/api/images/upload_form/avatar', // point to server-side controller method
             dataType: 'text', // what to expect back from the server
             cache: false,
             contentType: false,
             processData: false,
             data: form_data,
             type: 'post',
             success: function (response) {
                 $('#msg').html(response); // display success response from the server
                 },
             error: function (response) {
                 $('#msg').html(response); // display error response from the server
                 }
           });
     });
});

用户id=1<<我需要将此键和值添加到post url,但如何添加?

任何帮助都将不胜感激。

谢谢

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/46742
文章 [ 1 ]  |  最新文章 7 年前
Dave
Reply   •   1 楼
Dave    7 年前

对于那些仍像我一样挣扎的新人:

一。如何使用jquery附加图像文件并通过web api发布?

正在上载图像,但响应:{user_id'of undefined}?

在发布请求之前,必须从url获取参数。

var urlParams = new URLSearchParams(window.location.search); // current URL 

console.log(urlParams.get('user_id')); // return token id or user id

你要做的就是:

...
var user_data = urlParams.get('user_id'); // or token
form_data.append('user_id', user_data);
$.ajax({
        url: 'https://myurl/api/images/upload_form/avatar', 
        dataType: 'text', 
        cache: false,
        contentType: false,
        processData: false,
        data: form_data,
        ...

2.我怎样才能重新上传(更新)文件?

再上传一个文件!

可能不是正确的方法,但有效!