Copied to clipboard

Flag this post as spam?

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


  • Greg 25 posts 103 karma points
    May 24, 2017 @ 13:58
    Greg
    0

    Umbraco forms 6 and file upload

    Hello, I'm using Umbraco 7.6.1 and Umbraco Forms 6.0.1. I'm trying to use a recaptcha on a page with a file upload.

    I'm encountering 2 issues.

    1. When server side validation fails and the client is returned to the form. I am encountering this issue: http://issues.umbraco.org/issue/CON-1221 Instead of a Dictionary<string, string>, Model.Values is returning an IEnumerable<string> of the file location of the temp file upload. To handle this I modified the FieldType.FileUpload.cshtml file to look like this:

      @using Umbraco.Web
      @using Umbraco.Forms.Mvc
      
      
      @model Umbraco.Forms.Mvc.Models.FieldViewModel
      
      
      <input type="file" class="@Html.GetFormFieldClass(Model.FieldTypeName)"  name="@Model.Name" id="@Model.Id" multiple
      @if(Model.Mandatory){<text> data-val="true" data-val-required="@Model.RequiredErrorMessage"</text>} />
      
      
      @if (Model.Values != null && Model.Values.Any())
      {
          <p>
              <strong>Current File/s:</strong><br/>
              @*@foreach (Dictionary<string, string> dictionaryObject in Model.Values)
              {
                  //The model.values contains a list of dictionary objects with a single item
                  //With the key being the full path to the file
                  //And the value being the filename
                  if (dictionaryObject.Count > 0)
                  {
                      var fileInfo = dictionaryObject.First();
      
      
      
                  &lt;a href="@fileInfo.Key" target="_blank"&gt;@fileInfo.Value&lt;/a&gt;&lt;br/&gt;
      
      
                  &lt;input type="hidden" name="@(Model.Name)_file_@(fileInfo.Value)" value="@fileInfo.Key" /&gt;
      
      
              }
          }*@
      
      
          @foreach (var uploadObject in Model.Values)
          {
              var filename = Path.GetFileName(uploadObject.ToString());
      
      
              if (uploadObject != null)
              {
                  var fileInfo = uploadObject;
                  &lt;a href="@uploadObject.ToString().Substring(1)" target="_blank"&gt;@filename&lt;/a&gt;&lt;br /&gt;
                  &lt;input type="hidden" name="@(Model.Name)_file_@(filename)" value="@uploadObject" /&gt;
              }
          }
      &lt;/p&gt;
      
      }

    Which renders fine. However, I didn't realize until after that the value in the Model.Values is actually issue #2... they are temp files in the app_data/temp folders and therefore can't be linked.

    I'm not sure what else I can do at this point, any help would be appreciated.

  • Alex Clavelle 13 posts 124 karma points
    Jun 01, 2018 @ 14:46
    Alex Clavelle
    100

    Here's how I fixed this:

    @using Umbraco.Web
    @using Umbraco.Forms.Mvc
    
    @model Umbraco.Forms.Mvc.Models.FieldViewModel
    
    <input type="file" class="@Html.GetFormFieldClass(Model.FieldTypeName)"  name="@Model.Name" id="@Model.Id" multiple
    @if(Model.Mandatory){<text> data-val="true" data-val-required="@Model.RequiredErrorMessage"</text>} />
    
    @if (Model.Values != null && Model.Values.Any())
    {
        <p>
            <strong>Current File/s:</strong><br/>
            @foreach (object obj in Model.Values)
            {
                //if captcha validation fails, this will not be a dictionary of string,string
                var dictionaryObject = obj as Dictionary<string, string>;
                if (dictionaryObject == null)
                {
                    continue;
                }
            @*
            The model.values contains a list of dictionary objects with a single item
            With the key being the full path to the file
            And the value being the filename
            *@
                if (dictionaryObject.Count > 0)
                {
                    var fileInfo = dictionaryObject.First();
                    <a href="@fileInfo.Key" target="_blank">@fileInfo.Value</a><br/>
                    <input type="hidden" name="@(Model.Name)_file_@(fileInfo.Value)" value="@fileInfo.Key" />
                }
            }
        </p>
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft