Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Alicia 9 posts 89 karma points
    Apr 05, 2019 @ 12:03
    Alicia
    0

    Replacing media file without breaking the link to it - v8

    I'm new to Umbraco and I'm using the "Sample Site" on my PC - vsn 8.0.1. I've created a "Content Page" and added a "Full Width" row using the templates that came with the site. To the row I added an editor, and in the editor I created a link to the document "abcs.pdf", which I had already uploaded to Media > Documents > Classroom. The link worked fine. However, when I replaced "abcs.pdf" with a new version of the file (same exact name), the new version has a new URL but my link is still pointing to the old URL and gives a 404 error.

    The way I replaced the file was to navigate to it in the folder Media > Documents > Classroom, then "Remove" it, then upload the new file, then clicked "Save". Then I went to "Info" for the new file, right-clicked the URL and saw that the URL is different from the original file.

    Original file was: http://umbracov8-1.local/media/j32nuj0w/abcs.pdf

    New file is: http://umbracov8-1.local/media/di4piy42/abcs.pdf

    In the file system on my PC, the folder http://umbracov8-1.local/media/j32nuj0w is empty, so the original file was indeed "removed".

    I found a forum post from 2018 that describes this same problem but it's not helping me to resolve the issue. That post mentioned "grid" vs "media picker". I'm not familiar enough with Umbraco yet to know the difference.

    This is the old post: https://our.umbraco.com/forum/using-umbraco-and-getting-started/92806-replace-a-media-file

    Is the problem that the Sample Site is using "grid" ? What am I missing? How do I update a document without breaking a link to it?

  • Jonathan Distenfeld 105 posts 618 karma points
    Apr 06, 2019 @ 11:52
    Jonathan Distenfeld
    0

    Hello Alicia,

    why would you wan't to update a file and keep the old url? What is it you actually trying to do?

    In Umbraco you can use the Media Picker to pick a file. As you update the file, the media picker uses the new file instead so you normally don't have to care much about file urls.

    -Jonathan

  • Alicia 9 posts 89 karma points
    Apr 06, 2019 @ 13:33
    Alicia
    0

    Thank you Jonathan. We have many Word documents and PDF files that are resources on our site. We create links to them on various pages, with accompanying text to add value to the documents. The files sometimes need to be updated with newer content. We want to be able to update the files and not have to edit the links on the pages. As I said, I'm new to Umbraco ... I've been using Ektron for years. So maybe I'm not thinking correctly about how to achieve what we need? (I'm working in the 8.0.1 Sample Site for now.)

  • Jonathan Distenfeld 105 posts 618 karma points
    Apr 06, 2019 @ 15:17
    Jonathan Distenfeld
    0

    So you extended the media type for files with custom properties?

    I think the best way to achieve this, would be to create a docment type which would represent your file. It would have a property of editor type media picker to reference the actual file and your additional properties there.

    On your page you want to show the links, you can add a property with editor type nested content (or content picker) so you can add multiple instances of the document type.

    In your remplate you can then have access to the file, its url and additional properties. This way you don't have a problem with changing urls because the media picker also updates the url if you change the file.

    You don't have to create the document type. You also can add a multiple media picker to the page you want to show the links - that would also work.

  • Alicia 9 posts 89 karma points
    Apr 06, 2019 @ 20:32
    Alicia
    0

    Thank you again. Unfortunately I don't yet know enough about Umbraco to understand what you are suggesting. I did add a property called "contentDocument" to the document type "Content Base", and wired it to the page template with this:

    @Model.Value("contentDocument")

    When I edit my existing page, the media picker is there, and I navigate to the file I want and it gets added. But when I preview the page ... or publish it and then view it live ... all I see is this:

    Umbraco.Web.PublishedModels.File

  • Jonathan Distenfeld 105 posts 618 karma points
    Apr 07, 2019 @ 15:54
    Jonathan Distenfeld
    1

    Try it like so:

    @Model.ContentDocument.Url
    

    This should display the URL of the selected file. The snippet you're using returns the complete file type with all your properties. Url is one of the build-in properties of Umbraco.

  • Alicia 9 posts 89 karma points
    Apr 07, 2019 @ 22:37
    Alicia
    0

    Thank you again. I tried that ...

    @*

    @Model.Value("contentDocument")

    *@

    @Model.contentDocument.Url

    But I get a compilation error: Compiler Error Message: CS1061: 'ContentPage' does not contain a definition for 'contentDocument' and no accessible extension method 'contentDocument' accepting a first argument of type 'ContentPage' could be found (are you missing a using directive or an assembly reference?)

  • Jonathan Distenfeld 105 posts 618 karma points
    Apr 08, 2019 @ 06:21
    Jonathan Distenfeld
    1

    The ModelBuilder from Umbraco always turns the first letter from your propertyname uppercase. This is a C#-convention.

    so your Code would look like this:

    @Model.ContentDocument.Url
    

    Note that this is the strongly typed solution. You also can use Model.Value(), but I wouldn't recommend this. It would look like this:

    @Model.Value("contentDocument")
    

    but you might have to make sure the property has the correct type:

    @{ var file = Model.Value<Umbraco.Web.PublishedModels.File>("contentDocument") }
    @file.Url
    

    -Jonathan

  • Alicia 9 posts 89 karma points
    Apr 08, 2019 @ 12:30
    Alicia
    0

    Thank you again. I finally got it! Here's the syntax I ended up with:

    (I tried pasting in some HTML here but it didn't render properly ... how do I do that?) Anyways, I had an "a" tag with the href set to @Model.ContentDocument.Url and the title and link text set to @Model.ContentDocument.Name.

    I tested with just a single-item media picker, and the link does NOT break when the document is replaced by first "removing" it in Media and then uploading a file with the same name. (Yay!)

    I was just trying to understand the functionality, and now I do. Now, for multiple items, I will delve into how to extract them all ... I'm guessing I'll need some kind of a "For Each" or whatever.

    Thanks again. Big help!

  • Jonathan Distenfeld 105 posts 618 karma points
    Apr 08, 2019 @ 13:33
    Jonathan Distenfeld
    1

    Great to here you got it working!

    a (foreach-)loop is exactly what you need when having multiple files. You'll also want to allow multiple media items in your data type.

    -Jonathan

  • Alicia 9 posts 89 karma points
    Apr 08, 2019 @ 13:36
    Alicia
    0

    For future reference, how do I insert HTML code into a forum post here (like you did)? Thanks.

  • Jonathan Distenfeld 105 posts 618 karma points
    Apr 08, 2019 @ 13:49
    Jonathan Distenfeld
    100

    You can do this by clicking the "code-" icon below in the editor. It will look different in a seperate line and inside a text line like this,

    or this
    

    Also, could you please mark the post as solution that helped you solving your problem? It also helps other people having the same problem.

    Code-Icon

    -Jonathan

  • Alicia 9 posts 89 karma points
    Apr 08, 2019 @ 13:56
    Alicia
    0

    Thanks. I was looking for that icon but for some reason none of them were showing up. I see them now.

  • Richard Soeteman 4035 posts 12842 karma points MVP
    Apr 08, 2019 @ 07:22
    Richard Soeteman
    0

    Hi,

    Hope to release V2.8 of seochecker soon that supports Umbraco V8. SEOChecker will track url changes, also for media and creates a redirect automatically.

    Best,

    Richard

Please Sign in or register to post replies

Write your reply to:

Draft