Copied to clipboard

Flag this post as spam?

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


  • Paul Dowman 4 posts 35 karma points
    Aug 05, 2015 @ 12:51
    Paul Dowman
    0

    File upload from members - Umbraco 7

    Hi All,

    I'm looking to implement a file upload that logged in members can use to create simple content (Adverts, Photo Galleries etc).

    These are members, not users, so the upload needs to be done via .net code. I'm trying to implement this with a surface controller, however I'm stuck.

    I want to use a HTML5 file input type on the form. I can't work out how to get hold of that in my controller code.

    I've created a view model, and I've created a strongly typed "edit" view which passes though a FormColletion. However the file selected by the user is not part of that collection, other form elements are there, but not the file.

    I guess I have 2 questions. 1. Am I going about this the right way (I'm new to Umbraco) 2. How do I get the user selected file into my controller code?

  • Stefano Beretta 102 posts 247 karma points
    Aug 05, 2015 @ 13:13
    Stefano Beretta
    100

    Hi Paul,

    I alrady experienced this behaviour. Tell me if I'm wrong:

    • you already created a model within a HttpPostedFileBase property
    • the form is rendered directly in your typed view (and not into a typed partial view)
    • you already have your controller to manage the posted model

    If it is so could you try to move the whole form into a typed partial and put the partial into your not typed view?

    I had the same issue (I honestly don't know if it's a bug or not) and this worked for me.

    BR,

    Stefano

  • Paul Dowman 4 posts 35 karma points
    Aug 05, 2015 @ 13:26
    Paul Dowman
    1

    Many thanks Stefano.

    I think I'm getting closer. After posting the question here I decided to try a different tack and look at this as a Razor problem, rather than an Umbraco one. I found this article: http://stackoverflow.com/questions/304617/html-helper-for-input-type-file

    I've updated my model to use the HttpPostedFileBase (it was previously using a string to hold the umbraco media file path once it is saved as content), and I've changed the controller action method signature from

    public ActionResult Edit(int id, FormCollection collection) 
    

    to

    public ActionResult Edit(myViewModel model)
    

    I can now get a hold on the uploaded file in my controller, and upload it to Umbraco from there. Awesome. :)

    I'm currently using a typedPartial view for the edit screen - seems to be working OK. I haven't done much more than use the Visual Studio auto generated scaffold view. I'll beautify it after I get the logic written.

    Many thanks for your help.

Please Sign in or register to post replies

Write your reply to:

Draft