API to Download file from URL and attach with a Document field

We are getting an image link from WhatsApp API where we get a URL for the image . We would like to download the file and save it to ERPNEXt and then update the link to a field value

Nice!
Do you have an actual question or problem?

Thx for responding, I could upload file by using following method to save file and get file path to attach with the document.

My question is to seek advise if we have url instead of file data , how the file will be fetched from url and saves to File,

	_file = frappe.get_doc({
		"doctype": "File",
		"file_name": data.get("filename"),
		"is_private": 1,
		"content": data.get("filedata"),
		"decode": 1})
	_file.save(ignore_permissions = True)
	gen_response(200, "Profile Photo uploaded successfully", _file)

I think that can be managed using a python Requests package. You just have to call that path using the Get method and get the content of a file and save it into ERP.

Thanks for info, will try and update

Hi @CA_B.C_Chechani
you get any solution for that we face the same situation
We upload URLs for pics using the import tool
need to download in erpnext and remove the old link

My solution Was you will need to add an extra attached hidden field to download the image from the URL and then Save links to New Fields and then Fetch the Internal URL to the correct Attach image to remove outsourced link

This is working, need more test

def get_file_from_url(**kwargs):
data = frappe._dict(kwargs)
try:
	f =urlopen(data.get("link"))
	myfile = f.read()
	_file = frappe.get_doc({
		"doctype": "File",
		"file_name": data.get("filename"),
		"is_private": 1,
	
		"content": myfile,
		})
	_file.save(ignore_permissions = True)		
	gen_response(200, "Profile Photo uploaded successfully", _file)

except Exception as e:

    frappe.log_error(frappe.get_traceback())

    gen_response(500,"Something Wrong. Please Try Again")
1 Like

Try out code shared

1 Like