腾讯云存储的配置代码 略。。。
views.py
@csrf_exempt
def filea(request):
ret = {'status':False,'url':None, 'error':None}
if request.method == 'POST':
myFile = request.FILES.get("file", None)
if not myFile:
return JsonResponse((u'上传失败', 0), safe=False)
ret['status'] = True
response = client.put_object(
Bucket='aabc-1256236896',
Body=myFile,
Key='ok321/' + '%s.jpg' % int(time.time())
)
print response
#ret['url'] = data.url
return JsonResponse(json.dumps(ret), safe=False)
.html 文件代码
{% load static %}
<form method="post" enctype="multipart/form-data" action="" > {% csrf_token %}
<input type="file" name="file" onchange="up_zbimg(this.files[0])" ID="file" style="display: none"/>
<p style="margin:10px 0;" onclick="file.click()">上传图片</p>
</form>
<section class="fixed">
<span><img src="/{{ user.icon }}" onclick="file.click()"></span>
</section>
<script src="https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/js/lib/jquery-1.10.2_1c4228b8.js"></script>
<script>
// 上传头像
function up_zbimg(file){
xhr = new XMLHttpRequest();
xhr.open('post', "{% url 'zbinfo:filea' %}", true);
xhr.onload = function () {
data = xhr.responseText;
data = JSON.parse(data);
console.log(data);
$('.fixed img').attr('src', data['img'])
if(this.status == 200||this.status == 304){
alert(this.response);
}
};
formdata = new FormData();
formdata.append('file', file);
xhr.send(formdata);
}
</script>
怎么样在上传后,直接得到上传的图片url地址?
并在 html页面后显示出来?
现在的代码功能是可以上传成功,但是不知道怎么取得这个上传图片的url地址?