Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The input is not a valid Base-64 string as it contains a non-base 64 character?

Tags:

I have a form where a user can upload a file to the sites download section. However when the form is submitted I get this error, without the request ever making it to the action method.

"The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters."

Code:

[HttpPost]
    [Authorize]
    public ActionResult Create(Download dl, HttpPostedFileBase DownloadFile)
    {

And

@model Models.Download

@{
    ViewBag.Title = "Add Download";
}

<h3>Add Download</h3>

@using (Html.BeginForm("Create", "Download", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(true)

    <div class="editor-label">Download File</div>
    <div class="editor-field">
        <input name="DownloadFile" id="DownloadFile" type="file" />
        @Html.ValidationMessage("DownloadFile");
    </div>

    <div class="editor-label">@Html.LabelFor(model => model.Downloads)</div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Downloads)
        @Html.ValidationMessageFor(model => model.Downloads)
    </div>

    <div class="editor-label">@Html.LabelFor(model => model.DownloadDate)</div>
    <div class="editor-field">
        @Html.EditorFor(model => model.DownloadDate)
        @Html.ValidationMessageFor(model => model.DownloadDate)
    </div>

    <div class="display-field"><input type="submit" value="Add" /></div>
}

<div>@Html.ActionLink("Back To Downloads", "Index")</div>

Any sugestions?

Thanks, Alex.

like image 646
Alex Hope O'Connor Avatar asked Feb 26 '11 04:02

Alex Hope O'Connor


People also ask

What characters are not allowed in Base64?

The base 64 digits in ascending order from zero are the uppercase characters 'A' to 'Z', lowercase characters 'a' to 'z', numerals '0' to '9', and the symbols '+' and '/'. % is not allowed in base64 encoding. Save this answer.

What are valid Base64 characters?

Base64 only contains A–Z , a–z , 0–9 , + , / and = . So the list of characters not to be used is: all possible characters minus the ones mentioned above. For special purposes . and _ are possible, too.

How do you make a Base64 string?

If we were to Base64 encode a string we would follow these steps: Take the ASCII value of each character in the string. Calculate the 8-bit binary equivalent of the ASCII values. Convert the 8-bit chunks into chunks of 6 bits by simply re-grouping the digits.

How do you know if a string is Base64?

In base64 encoding, the character set is [A-Z, a-z, 0-9, and + /] . If the rest length is less than 4, the string is padded with '=' characters. ^([A-Za-z0-9+/]{4})* means the string starts with 0 or more base64 groups.


3 Answers

Ok I figured this out finally, It was all caused because I named the file input on the form the same as my models file field, so the model binder was picking this up and trying to bind the posted file directly to the binary property which was throwing an exception because the string was not binary.

So to fix it I simply added this to my create action method:

[HttpPost]
    [Authorize]
    public ActionResult Create([Bind(Exclude = "DownloadFile")] Download dl, HttpPostedFileBase DownloadFile)
    {

By telling the model binder to exclude the field it solved the problem.

Thanks, Alex.

EDIT: This could also easily be solved by using view models

like image 176
Alex Hope O'Connor Avatar answered Oct 06 '22 00:10

Alex Hope O'Connor


Alex, you are partially correct with your assessment. The reason why it fails when you have a property of the same name on your model as the name of the input object on the form is due to the fact that the DataType of the matchingly-named property on your model is not System.Web.HttpPostedFileWrapper which is the datatype that the binary-binder will attempt to perform a bind to.

Excluding your property by using the Bind attribute and then extracting the file from the Request.Files collection as you demonstrated may work, but it may be more elegant to let the binder do its thing (provided that the data type is matching as I mentioned above) and then you can simply access the file directly from the property of your model

like image 42
Awah Teh Avatar answered Oct 05 '22 23:10

Awah Teh


You could try removing the HttpPostedFileBase from the controller method and use Request.Files[0] and see if that makes a difference.

Honestly though I dont'see why this would be failing unless there is something that is causing it within your Model.

Also, nit picking here but DownloadFile should be downloadFile on your form and in your controller method.

like image 43
Mike Geise Avatar answered Oct 06 '22 00:10

Mike Geise