How to upload image to erpnext using ReST API

hi
http://erpnext-dev.ic-zedach.de/api/method/run_custom_method?cmd=uploadfile&doctype=Item&docname=5840014&filename=5840014_002.jpg&filedata={ Filedata in Base64}&from_form=1|.

i was reading a thread here

i have tried to encode base64 … but nothing works … !!

it works perfectly with files txt … but images !!

^up ^up ^up ^up ^up ^up

import json
import base64
import os

path = os.path.abspath(__file__)
path = path.rsplit('/apps/', 1)[0]


file_url3 = frappe.get_value("File", {"file_name":doc.sales_contract.rsplit('/', 1)[1],
									 "attached_to_doctype":"yourdoctype"}, "file_url")
is_private3 = frappe.get_value("File", {"file_name":doc.sales_contract.rsplit('/', 1)[1],
									 "attached_to_doctype":"yourdoctype"}, "is_private")

if is_private3 == 1 :
	url3 = path+"/sites/yoursitename"+str(file_url3)
else:
	url3 = path+"/sites/yoursitename/public"+str(file_url3)


with open(url3, "rb") as image_file3:
	encoded_string3 = base64.b64encode(image_file3.read())
client = FrappeClient("http://xx.xx.x.xx", "user", "password")

doc_from_server = client.get_value("yourdoctype", "name", {"name1": doc.name1})

res = client.session.post(client.url + "/api/method/run_custom_method",
data={	
	'cmd': 'uploadfile',
	'doctype': 'yourdoctype',
	'docname': doc_from_server["name"],
	'filename': doc.sales_contract.rsplit('/', 1)[1],
	'filedata': encoded_string,
	# 'file_url':doc.delivery_order,
	'from_form':1,

})

if doc_from_server:
	new_doc = client.get_doc("yourdoctype", doc_from_server["name"])
else:
	new_doc = {"doctype":"yourdoctype"}



new_doc["name1"] = doc.name1
new_doc["sales_contract"]=doc.sales_contract

if doc_from_server:
	client.update(new_doc)
	print "Updated " + new_doc["name1"]
else:
	client.insert(new_doc)
	print "Inserted " + new_doc["name1"]
3 Likes

i will clean this code but it works right now with me for PDF and Images

after that i will try to PR it to Frappe.client

2 Likes