gpt4 book ai didi

python - 如何将文件从 Mayan EDMS 发送到外部 api?

转载 作者:行者123 更新时间:2023-12-01 08:23:25 27 4
gpt4 key购买 nike

我还在学习 Django,还有很多未知的东西。

问题是我无法将 .pdf(或任何其他格式)通过 ajax post 方法发送到外部 API。所以在接收端我只得到文件的字符串位置而不是实际的文件。

我已将以下 JavaScript 代码放入 generic_list_items_subtemplate.html

{% load i18n %}
{% load static %}

{% load common_tags %}
{% load navigation_tags %}

<div class="row">
<div class="col-xs-12">
<h4>
{% if page_obj %}
{% if page_obj.paginator.num_pages != 1 %}
{% blocktrans with page_obj.start_index as start and page_obj.end_index as end and page_obj.paginator.object_list|length as total and page_obj.number as page_number and page_obj.paginator.num_pages as total_pages %}Total ({{ start }} - {{ end }} out of {{ total }}) (Page {{ page_number }} of {{ total_pages }}){% endblocktrans %}
{% else %}
{% blocktrans with page_obj.paginator.object_list|length as total %}Total: {{ total }}{% endblocktrans %}
{% endif %}
{% else %}
{% blocktrans with object_list|length as total %}Total: {{ total }}{% endblocktrans %}
{% endif %}
</h4>
<hr>
<div class="well center-block">
<div class="clearfix">
<div class="pull-right">
<form action="{% url 'common:multi_object_action_view' %}" class="form-multi-object-action" method="get">
{% if object_list %}
{% if not hide_multi_item_actions %}
{% get_multi_item_links_form object_list %}
{% endif %}
{% if multi_item_actions %}
<fieldset style="margin-top: -10px;">
<input class="check-all" type="checkbox"/>&nbsp;
{{ multi_item_form }}
</fieldset>
{% endif %}
{% endif %}
</form>
</div>
</div>

{% if object_list %}
<hr style="border-bottom: 1px solid lightgrey;">

{% endif %}


<div class="row row-items">
{% for object in object_list %}
<div class="{{ column_class|default:'col-xs-12 col-sm-4 col-md-3 col-lg-2' }}">
<div class="panel panel-primary panel-item">
<div class="panel-heading">
<div class="form-group" id="myform">

<div class="checkbox">
<label for="id_indexes_0">
{% if multi_item_actions %}
{% if multi_select_item_properties %}
<input class="form-multi-object-action-checkbox check-all-slave checkbox" type="checkbox" name="properties_{{ object|get_encoded_parameter:multi_select_item_properties }}" />
{% else %}
<input class="form-multi-object-action-checkbox check-all-slave checkbox" type="checkbox" name="{{ object.get_absolute_url }}" />
{% endif %}
{% endif %}

<span style="color: white; word-break: break-all; overflow-wrap: break-word;">
{% if not hide_object %}
{% if main_object %}
{% with object|object_property:main_object as object %}
{% if not hide_link %}<a name="test" href="{{ object.get_absolute_url }}">{{ object }}</a>{% else %}{{ object }}{% endif %}
{% endwith %}
{% else %}
{% if not hide_link %}<a href="{{ object.get_absolute_url }}">{{ object }}</a>{% else %}{{ object }}{% endif %}
{% endif %}
{% endif %}

</span>
</label>
</div>
</div>

</div>
<div class="panel-body">

{% if not hide_columns %}
{% for column in object|get_source_columns %}
<div class="text-center" style="">{% source_column_resolve column=column %}{{ column_result }}</div>
{% endfor %}
{% endif %}

{% for column in extra_columns %}
<div class="text-center"><span class="list-extra-column-label">{{ column.name }}</span>: {{ object|object_property:column.attribute }}</div>
{% endfor %}

{% if not hide_links %}
<p class="text-center">
{% get_menu_links 'object menu' source=object as resolved_links %}
{% for object_navigation_links in resolved_links %}
{% with 'true' as as_dropdown %}
{% include 'navigation/generic_navigation.html' %}
{% endwith %}
{% endfor %}
</p>
{% endif %}

</div>
</div>
</div>
{% empty %}
<div class="col-xs-12">
{% include 'appearance/no_results.html' %}
</div>

{% endfor %}

</div>

<!--</form>-->




{% include 'pagination/pagination.html' %}
</div>
</div>
</div>

<!--{% if object_list %}-->
<form method="post">
{% csrf_token %}
<input type="hidden" name="csrf-token" value="nc98P987bcpncYhoadjoiydc9ajD1cn">
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-3">
<button id="sendBTN" class="btn btn-success" type="submit">Send checked documents to another database</button>
</div>
<div class="col-md-3"></div>
</div>
</form>

<br>
<br>
<br>
<!--{% endif %}-->


<script type="text/javascript">
'use strict';

var $data = [];

$(document).on("click", "input[type='checkbox']", (e) => {
var name = $(e.target).attr("name");
if($(e.target).prop("checked")){
$data.push(name);
}else{
$data = jQuery.grep($data, function(value) {
return value != name;
});
}
console.log($data);
});


function csrfSafeMethod(method)
{
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}

$.ajaxSetup({
beforeSend: function (xhr, settings) {
if(!csrfSafeMethod(settings.type) && !this.crossDomain)
{
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});



document.getElementById("sendBTN").onclick = function() {

var form_data = new FormData();
// form_data.append('file', $('#myform').get(0));

form_data.append('file', $data);

console.log(form_data);

$.ajax({
type: "POST",
// contentType: "application/x-www-form-urlencoded",
contentType : false,
processData: false,
url: "http://192.168.10.22:5000/api/FileRecive/GetDocument",
dataType: "json",
data: form_data,
success: function (data) {
data = $.parseJSON(data);
alert("success");
},
error: function (xhr) {
alert("error - " + xhr.statusText);
}
});
};


$(function() {
$('.row-items > [class*="col"] .panel-item .panel-heading').matchHeight();
$('.row-items > [class*="col"] .panel-item .panel-body').matchHeight();
$('.row-items > [class*="col"] .panel-item').matchHeight();
});
</script>

在接收端我得到

name = "file"
value = "/documents/12/preview/"

但我想获取文件而不是这个字符串。我不知道如何定位文件以直接发送它。

enter image description here

最佳答案

URL“/documents/12/preview/”是使用用户界面查看文档的 URL。如果你想要的是文档的实际文件,你需要使用Mayan的API。由于 Mayan 中的文档是版本的集合,因此您需要所需文档的最新版本的 ID。为此,文档详细信息的 API URL 为:

“/api/documents/271/”

这会给你这样的东西:

enter image description here

从那里查找字典键“latest_version”,然后查找键“download_url”。截图中的下载地址为“http://127.0.0.1:8000/api/documents/271/versions/267/download/”。对此 URL 执行 GET 请求即可获取文档的实际文件。将文件保存在临时变量(或 Javascript Blob 文件对象 https://developer.mozilla.org/en-US/docs/Web/API/Blob )中,然后通过 POST 请求将其发送到您的自定义 API。

另一种选择是仅将“download_url”发送到您的 API,并让工作进程获取文档数据,这样您就不必将文档的二进制数据存储在浏览器内存中。

有关 Mayan API 的更多信息,请转到“工具”->“API 文档”,您将获得一个 API 文档 View ,允许使用安装中的实时数据测试 API。

enter image description here

关于python - 如何将文件从 Mayan EDMS 发送到外部 api?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54464565/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com