I stored image file in path djangor_prject_name/pictures/image_name.jpg by using
models.ImageField(upload_to='pictures/services/', max_length=255, null=True, blank=True)
and file is uploaded successfully.
But while retrieving, what is the exact url to view image?
Because id Database, the value is just
picutes/services/image_name.jpg
So, in my view file, I am using vue.js and need full path or url of the image to display.
After creation it won't give you full url, but in the list it will come with full url. So for that you use custom serializers.SerializerMethodField.
class TestSerializer(serializers.ModelSerializer):
photo = serializers.SerializerMethodField()
class Meta:
model = Test
fields = ('photo',)
def get_photo(self, car):
request = self.context.get('request')
photo = Test.photo.url
return request.build_absolute_uri(photo)
For further details refer here
Related
I have a api and this api returning a binary pdf.
This pdf is changing everytime when a request come and i don't want the store it.
Simply i want the create a button and this should trigger a function.
Function should be take my api response (binary pdf) and show this pdf to user in interface. (with download button)
Thanks for any suggestion
I would write a controller to return the file, and put a button on the view or in the header with action to call the controller.
from odoo.http import request, content_disposition, Controller
class MyController(Controller):
#http.route('/download_file', auth='user', type='http')
def download_file(self, id, **kw):
# call the external API to retrieve the file
file_content = requests.get('http://example.com/api/file').content
# set the filename and content type
filename = 'my_file.txt'
content_type = 'text/plain'
# send the file as a response to user and prompt them to download it
return request.make_response(file_content,
[('Content-Type', content_type),
('Content-Disposition', content_disposition(filename))])
I have to set a image on wizard dialogue in swt,classpath load this image but image not displayed properly. In my design create a composit in this composit create a lebel.there after in this lebel I want add an image.After that i will create a runnable jar.This jar will be executed with proper image. In my implementation part i have create a resources (src folder) within src folder and in this resources folder kept image which i need.Please find the below code,
File file = new File("resources/Automatics_Logo.png");
Image image = new Image(Display.getDefault(), file.getPath());
Label lblNewLabel_4 = new Label(composite, SWT.NONE);
lblNewLabel_4.setImage(SWTResourceManager.getImage(CheckSystemConfigurationAndInstallation.class,image.toString()));
Image imgSWT=null; // Image class is the SWT Image class
ImageDescriptor imgDesc=null;
java.net.URL imgURL = IntroductionPage.class.getResource("/Automatics_Logo.png");
if (imgURL != null) {
imgDesc = ImageDescriptor.createFromURL(imgURL);
imgSWT = imgDesc.createImage();
I'm using node.js to generate a direct upload file tag for Cloudinary
cloudinary.uploader.image_upload_tag('coverImage', {html: {'class': 'cloudinary-coverImage', format: 'jpg'}})
After uploading, a hidden input tag like this is auto generated at the end of the form:
<input type="hidden" name="coverImage" value="image/upload/v1431361091/bns8et8ksrx3km5esdpj.jpg#d36285fd9bcccd5a2034b22ebf69d867fcee0bbc">
I realize that the input value is not exactly the public id like said in the docs, so when I store it in the DB, this will not generate a proper image
cloudinary.image(abc.coverImage)
How can I get the image's exact public id in the hidden input value?
TL;DR
The format of the hidden input tag's value is {resource_type}/{type}/v{version}/{public_id}.{format}#{signature}. You need the public_id, which in your example is bns8et8ksrx3km5esdpj.
Details
The value of the hidden field includes the resource type, version, public id and the signature of the uploaded image. The public_id is the last part of the URI before the Hash (#).
Refer to the sample code, taken from the node.js controller code, for an example of handling the upload results (to match your example above, image_id below should be replaced with the name you designated for the field, i.e. coverImage):
var Photo = schema.models.Photo;
var photo = new Photo(req.body);
var image = new cloudinary.PreloadedFile(req.body.image_id);
// check that image resolved from image_id is valid
if (image.is_valid()){
photo.image = image.toJSON();
console.dir(photo.image)
}
photo.save().then(function(photo){
console.log('** photo saved')
})
Brower side
If you need to get the public_id of the image following a direct upload on the browser side, you should listen to the "cloudinarydone" event:
$("body").on( "cloudinarydone", function(e, data) {
console.log(data.result.public_id);
});
I have a webmail and i want the user to press on a picture in the webmail and then be linked back to my mvc project view. I don't have a real domain yet so i can't type www.mysite.com. I want to be able to link to the view without depending on which domain the site is currently running on.
in my webmail i have the body that looks like this:
string Body = "<b>Välj ett alternativ!</b><br><br><a href='cid:path4'/><img src='cid:Happy'/ alt='HTML tutorial' width='120' height='120'></a><input type='image' src='cid:Orange'/ name='image' width='120' height='120'><input type='image' src='cid:Mad'/ name='image' width='120' height='120'>";
where the cid:path4 is i want the url och what ever so that i linkes me back to my project.
You'll have to format the absolute path into the body text from your controller action (or pass it through to whatever method generates the body):
var path = Request.Uri.AbsoluteUri;
var emailBody = String.Format("Your email body. Link", path);
Or, you could link to another controller action with Url.Action:
var path = Url.Action("MyAction", "MyController", null, "http");
var emailBody = String.Format("Your email body. Link", path);
Of course, this all assumes that this email is sent by a controller action - if it's not, it may not be possible to determine the absolute URI of the website.
Im trying to post an activity with an image to a google+ domain with the new google + domains API
Posting an activity is working fine but when i try to attach a photo to it, i receive a 500 error with null description.
This is the code:
String msg = "Activity with photo";
// Create a list of ACL entries
PlusAclentryResource resource = new PlusAclentryResource();
resource.setType("domain"); // Share to domain
List<PlusAclentryResource> aclEntries = new ArrayList<PlusAclentryResource>();
aclEntries.add(resource);
Acl acl = new Acl();
acl.setItems(aclEntries);
acl.setDomainRestricted(true); // Required, this does the domain restriction
// Create a new activity object
Activity activity = new Activity()
.setObject(new Activity.PlusObject().setOriginalContent(msg))
.setAccess(acl);
// Attach the link
Activity.PlusObject.Attachments attachment = new Activity.PlusObject.Attachments();
attachment.setObjectType("photo");
attachment.setUrl( "http://c299813.r13.cf1.rackcdn.com/MuseeduLouvre_1335428699_org.jpg" );
attachment.setId( randomId ); //if not specified, google returns an error with "you must specify the photo id"
List<Activity.PlusObject.Attachments> attachments = new ArrayList();
attachments.add(attachment); // You can also add multiple attachments to the post
activity.getObject().setAttachments(attachments);
activity = plus.activities().insert("me", activity).execute();
When the code calls the execute, i receive this error:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 500
{
"code": 500,
"message": null
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
The same code but with the attachemnt lines commented works fine. Has someone managed to create an activity with an image? any clue?
Thanks in advance.
Directly attaching a photo via URL isn't possible. The process works slightly differently, as described here: https://developers.google.com/+/domains/posts/attaching-media
If you don't have the actual binary data of the photo you will first have to "download" the photo. Then you can upload the actual photo data via the media().insert method, which will give you the Photo Id which you can then use in attachment.setId().
setUrl isn't necessary in this case.
If you want to attach a photo as URL, this could also be handled like an article attachment (same as if you would just copy/paste the URL into a Google+ post). In that case you would use attachment.setObjectType("article") and only set the Url. The id isn't necessary in this case.