简体   繁体   English

如何将多个值添加到输入标签 - js

[英]how to add multiple values to an input tag - js

i'm trying to capture multiple images and then save into the database here is the js code我正在尝试捕获多张图像,然后保存到数据库中,这里是 js 代码

 const player = document.getElementById('player') const canvas = document.getElementById('canvas') const form = document.getElementById('form') const docs = document.getElementById('documents') const captureButton = document.getElementById('capture') const context = canvas.getContext('2d') captureButton.addEventListener('click', (e) => { context.drawImage(player, 0, 0, canvas.width, canvas.height) e.preventDefault(); let new_image = document.createElement('img') new_image.src = canvas.toDataURL() console.log(new_image) docs.value = canvas.toDataURL() form.insertAdjacentElement('beforeend',new_image) e.preventDefault(); }); navigator.mediaDevices.getUserMedia({video: true}).then((stream) => { player.srcObject = stream;})
 <form id="form" action="" method="POST" enctype="multipart/form-data">{% csrf_token %} <video id="player" controls autoplay></video> <button type="button" id="capture">Capture</button> <button>save</button> <canvas id="canvas" width=320 height=240></canvas> <input type="text" name="documents" hidden id="documents"> </form>

and here is my views.py to get multiple base64 data and split it by using its data attribute, here is my code这是我的 views.py 获取多个base64数据并使用其data属性拆分它,这是我的代码

@login_required
def add_new_image(request,id):
    obj = get_object_or_404(Booking,id=id)
    if request.method == 'POST':
        images = request.POST.get('documents')
        data,imgdata = images.split('data')
        for img in imgdata:     
            print(img)       
            format, imgstr = img.split(';base64,') 
            ext = format.split('/')[-1] 
            data = ContentFile(base64.b64decode(imgstr), name='temp.' + ext)
            photo = Document.objects.create(
                booking = obj,
                docs = data                
            )
            photo.save()
        return redirect(reverse_lazy("booking:add_booking",kwargs={"room_no":obj.room_no.room_no}))
    else:
        messages.error(request,_('choose or capture right image ..'))
    return render(request,'booking/add_img.html',{'obj':obj})

but it raise this error:但它引发了这个错误:

not enough values to unpack (expected 2, got 1)没有足够的值来解压(预期 2,得到 1)

and the print(img) returns only: !并且print(img)只返回:! and when i remove the for loop and add this format, imgstr = imgdata.split(';base64,') it saves only the last image, please is there any way to achieve it?当我删除 for 循环并添加此format, imgstr = imgdata.split(';base64,')它只保存最后一张图片,请问有什么办法可以实现吗? to add all base64 data from js to input tag and then split the input data by its data base64 attribute.将 js 中的所有 base64 数据添加到输入标签,然后通过其数据 base64 属性拆分输入数据。 thanks in advance..提前致谢..

Where is your template snip, I assume you're not using ajax here so if you're having multiple image tags you would do something like this你的模板片段在哪里,我假设你没有在这里使用 ajax 所以如果你有多个图像标签你会做这样的事情

<img src='image01' name=image' class =" something"/>
<img src='image02' name=image' class =" something"/>
<img src='image03' name=image' class =" something"/>

images = request.FILES.getlist('image')

then returns a list of images and consequently you can save them in you database然后返回图像列表,因此您可以将它们保存在数据库

  • You need to create <input> , not <image>您需要创建<input> ,而不是<image>

  • You're not appending anything by doing docs.value = canvas.toDataURL() , just overwriting old values.您不会通过执行docs.value = canvas.toDataURL()来附加任何内容,只是覆盖旧值。


 const player = document.getElementById('player') const canvas = document.getElementById('canvas') const form = document.getElementById('form') const captureButton = document.getElementById('capture') const context = canvas.getContext('2d') captureButton.addEventListener('click', (e) => { context.drawImage(player, 0, 0, canvas.width, canvas.height) let new_data = document.createElement('input') // <== use <input> new_data.setAttribute('name','document') new_data.value = canvas.toDataURL() form.insertAdjacentElement('beforeend',new_data) }); navigator.mediaDevices.getUserMedia({video: true}).then((stream) => { player.srcObject = stream;})
 form *{max-width:20vw;} img{display:inline-block;} canvas{display:none;}
 <form id="form" action="" method="POST" enctype="multipart/form-data"> <video id="player" controls autoplay></video> <button type="button" id="capture">Capture</button> <button>save</button> <canvas id="canvas" width=320 height=240></canvas> </form>

https://jsfiddle.net/c7ykg83h/ https://jsfiddle.net/c7ykg83h/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM