Beispiel #1
0
def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.user.pk, request.FILES['file'])
            return {'form': form, 'url_to_file': '/media/files/%s_%s' %(request.user.pk, request.FILES['file'])}

    else:
        form = UploadFileForm()
    return {'form': form}
Beispiel #2
0
def index(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            # do stuff with request.FILES['file']
            # redirect to /UUID
            return HttpResponseRedirect('/')
    else:
        form = UploadFileForm()
    return render_to_response('upload/index.html',
                              {'form': form},
                              context_instance=RequestContext(request))
Beispiel #3
0
def upload_file(request):
    form = UploadFileForm()
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            uploaded_data = form.save(commit=False)
            uploaded_data.file_byte = form.cleaned_data['csv_file'].file.read()
            uploaded_data.save()
            return redirect('/')
        else:
            return render(request, 'upload.html', {'form': form})
    return render(request, 'upload.html', {'form': form})
Beispiel #4
0
def home(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            new_file = UploadFile(file=request.FILES['file'])
            new_file.save()

            return HttpResponseRedirect(reverse('upload:home'))
    else:
        form = UploadFileForm()

    data = {'form': form}
    return render(request, 'website/upload_page.html', data)
Beispiel #5
0
def upload_img(request):
    if request.method == 'POST':
        print('haha')
       
        form = UploadFileForm(request.POST, request.FILES)
        
       
        if form.is_valid():
            data = form.cleaned_data
            print (data)
            f = request.FILES['file']
            f1 = request.FILES['file1']
            kind = f.name.split('.')
            print (type(f))
            ck =['jpg', 'jpx', 'png', 'gif', 'webp', 'cr2', 'tif', 'bmp', 'jxr', 'psd', 'ico', 'heic', 'jfif'] 
            dot =kind[len(kind)-1]
            if dot in ck:
                if (f and f1):
                    print("haha")
                img1 = PIL.Image.open(f)
                img2 = PIL.Image.open(f1)
                wr = Wrapper()
                data = wr.calculate_similarity(img1, img2)
                print(data)
                # # check = createFolder(url_img'/')
                with open(url_img  + f.name, 'wb+') as destination:
                    for chunk in f.chunks():
                        destination.write(chunk)
                # # image 
                img_base64 = ""
                with open(url_img  + f.name, "rb") as img_file:
                    my_string = base64.b64encode(img_file.read())
                    img_base64 = 'data:image/png;base64,%s' % (my_string.decode('utf-8'))
                # print (type(f))
                with open(url_img  + f1.name, 'wb+') as destination:
                    for chunk in f1.chunks():
                        destination.write(chunk)
                # # image 
                img_base641 = ""
                with open(url_img  + f1.name, "rb") as img_file:
                    my_string = base64.b64encode(img_file.read())
                    img_base641 = 'data:image/png;base64,%s' % (my_string.decode('utf-8'))
                
                return render(request, 'upload.html', {'dt': img_base64, 'dt1': img_base641 , 'num': data})
            else:
                # not image
                messages.info(request, 'Plese input file is image!')
                return render(request, 'name.html')
    else:
        form = UploadFileForm()
    return render(request, 'name.html', {'form': form})
Beispiel #6
0
def upload(request):
    """
    Обработчик AJAX запроса загрузки файла.

    Если форма валидна, запускает процесс загрузки файла,
    иначе, возвращает информацию об ошибках формы.
    
    """
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            _upload_file(request.FILES['uploadfile'])
            return JsonResponse({'ok': ''}, content_type='text/html')
        else:
            return JsonResponse({'error': dict(form.errors)})
    else:
        return JsonResponse({'error': 'POST method expected'})
Beispiel #7
0
def index(request):
    """
    use: 
        return index upload view
    params: 
        request: the request from client
    return:
        HttpResponse: the result for upload file (json)
    """
    error_lang = globalconf.DEFAULT_ERROR_LANG
    result_code = ''

    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)

        if form.is_valid():
            model = form.save(commit=False)
            path = form.cleaned_data['path']
            params = form.cleaned_data['params']
            error_lang = get_error_lang(form)

            result_code, model= upload_check_params(model, path, request)
            # '1000': 'normal'
            if result_code == '1000':

                dev_info, file_info = get_dev_file_info(model)
                upload_plugins_run(dev_info, file_info, params)
                model.save()

                logger.info('upload file success')
                logger.info('filename:%s client_ip:%s path:%s'%\
                            (model.filename, model.clientip, model.file.path))
        else:
            # '1008': 'upload form insufficient parameters'
            result_code = '1008'
    else:
        # '1006': 'request method error, must be POST'
        result_code = '1006'

    return HttpResponse(json.dumps(get_error_info(error_lang, result_code)))