html代码:
<h1>Uploading a photo.</h1>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>
<script type="text/javascript">
var p;
var canvas = document.createElement("canvas");
var img1=document.createElement("img");
function converttobyte()
{
p=document.getElementById("file").value;
img1.setAttribute('src', p);
canvas.width = img1.width;
canvas.height = img1.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img1, 0, 0);
var dataURL = canvas.toDataURL("image/png");
alert("from getbase64 function"+dataURL );
return dataURL;
}
</script>
<form action="UploadFile" method="post" accept-charset="utf-8" name="UploadFileForm"
enctype="multipart/form-data">
<input type="file" id="file" name=""><br>
<input type="submit" value="Upload" onclick="converttobyte">
</form>
Python代码:
def UploadFile(request):
if request.method == 'POST':
converttobyte = request.GET['converttobyte'].decode("base64")
#file = request.FILES['avatar']
default_storage.save("%s"%(converttobyte), ContentFile(converttobyte.read()))
return HttpResponse("File uploaded successfully")
else:
return HttpResponse("please upload a file")
应该在request.method== 'post'
之后添加的语句应该在python脚本中接受函数返回值。
请帮帮我。提前致谢
答案 0 :(得分:1)
在表单中创建隐藏变量
<form action="UploadFile" method="post" accept-charset="utf-8" name="UploadFileForm"
enctype="multipart/form-data">
<input type="hidden" id="url" name="url" value="">
<input type="file" id="file" name=""><br>
<input type="submit" value="Upload" onclick="converttobyte">
</form>
而不是从java脚本返回值,而是将该值赋给隐藏变量
function convert_to_byte()
{
p=document.getElementById("file").value;
img1.setAttribute('src', p);
canvas.width = img1.width;
canvas.height = img1.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img1, 0, 0);
var dataURL = canvas.toDataURL("image/png");
alert("from getbase64 function"+dataURL );
document.getElementById("url").value = dataURL;
}
然后在提交后,您可以使用以下代码获取python中的值,
if request.method == 'POST':
url_value = request.POST['url']
答案 1 :(得分:0)
解决问题的一种方法是:
创建隐藏字段,如下所示
<input type="hidden" id="hide" name="URL" value="">
在javascript函数中,不是返回值,而是将其设置为隐藏字段
document.getElementById("hide").value = dataURL;
提交表单后,隐藏的值随之而来。使用python代码中的request.GET ['URL']
希望这能解决问题。
答案 2 :(得分:0)
您的代码存在一些问题。
你不应该像onclick="converttobyte()"
那样调用方法吗?
在converttobyte()
方法中,您只是在提交表单时返回dataURL
,但该值是如何传递给POST请求的?
您需要使用POST请求传递值。您可以使用隐藏字段来实现此目的。尝试这样的事情:
<强>使用Javascript:强>
<h1>Uploading a photo.</h1>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>
<script type="text/javascript">
var p;
var canvas = document.createElement("canvas");
var img1=document.createElement("img");
function converttobyte()
{
p=document.getElementById("file").value;
img1.setAttribute('src', p);
canvas.width = img1.width;
canvas.height = img1.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img1, 0, 0);
var dataURL = canvas.toDataURL("image/png");
alert("from getbase64 function"+dataURL );
/**Set the value of the raw_bytes input field*/
var form = document.getElementById('uploadForm');
form.raw_bytes.value = dataURL;
}
</script>
<form id="uploadForm" action="UploadFile" method="post" accept-charset="utf-8" name="UploadFileForm"
enctype="multipart/form-data">
<input type="file" id="file" name=""><br>
<input type="hidden" name="raw_bytes" id="raw_bytes" />
<input type="submit" value="Upload" onclick="converttobyte()">
</form>
Django查看:
def UploadFile(request):
if request.method == 'POST':
raw_bytes = request.POST['raw_bytes'].decode("base64")