# Uploading files

**URL:** <https://forum.radzen.com/t/uploading-files/616>\
**Category:** Angular\
**Created:** [October 29, 2018, 7:04pm UTC](https://forum.radzen.com/t/uploading-files/616 "2018-10-29T19:04:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshwilliam](https://forum.radzen.com/user_avatar/forum.radzen.com/joshwilliam/32/538_2.png) [@joshwilliam](https://forum.radzen.com/u/joshwilliam)\
**Post date:** [October 29, 2018, 7:04pm UTC](https://forum.radzen.com/t/uploading-files/616/1 "2018-10-29T19:04:39Z")

</div>

The upload files documentation is a little unclear to me on usage. When I try the sample code from the documentation I can select a file and when I click upload the file is gone and I can no longer see it.

Where are the files stored when uploaded? Is it stored in the database?

How would someone go about setting up a file upload for a specific record such as an order (so you could store supporting documentation such as a pdf or excel file.).

---

<div class="post-metadata">

**Author:** ![enchev](https://forum.radzen.com/user_avatar/forum.radzen.com/enchev/32/16_2.png) [@enchev](https://forum.radzen.com/u/enchev)\
**Post date:** [October 30, 2018, 8:12am UTC](https://forum.radzen.com/t/uploading-files/616/2 "2018-10-30T08:12:05Z")

</div>

Hi Josh,

Indeed our article shows just how to post the file to the server, for more info how to save it please check this article:

> **[Upload files in ASP.NET Core](https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-2.1)**
>
> How to use model binding and streaming to upload files in ASP.NET Core MVC.

Best Regards,  
Vladimir

---

<div class="post-metadata">

**Author:** ![SeaderGamer](https://forum.radzen.com/user_avatar/forum.radzen.com/seadergamer/32/455_2.png) [@SeaderGamer](https://forum.radzen.com/u/SeaderGamer)\
**Post date:** [October 30, 2018, 5:25pm UTC](https://forum.radzen.com/t/uploading-files/616/3 "2018-10-30T17:25:18Z")

</div>

Hi Josh, do you want to save to a database? I've done this by adding a "file" field in the form, if you check this field, it passes the "dataURL" of the file, so I'm using up until now a varchar (max) in the database.  
To display these downloadable values ​​I've made a template with  
`<a href="this.safeURL(item.Annexo)">download</a>`  
being that the "safeURL" is a function using the sanitizer to say that url is safe

 ![Sem%20t%C3%ADtulo](https://forum.radzen.com/uploads/default/original/1X/f223aa5c2158bc6b189a34bfeab3d83b45d5371e.png)  
You have a question of mine here too, is there a way to use the varbinary of sql server to save it? I believe that my way will become obsolete with large files

---

<div class="post-metadata">

**Author:** ![Werther](https://forum.radzen.com/user_avatar/forum.radzen.com/werther/32/12818_2.png) [@Werther](https://forum.radzen.com/u/Werther)\
**Post date:** [November 21, 2018, 6:36pm UTC](https://forum.radzen.com/t/uploading-files/616/4 "2018-11-21T18:36:29Z")

</div>

Hi enchev,

I'm trying to upload files to a varbinary(max) field in sql server but it isn't working.  
When I submit the add page the model of page in the backend is null:

 ![image](https://forum.radzen.com/uploads/default/original/1X/af41eb677398d80d0be20511774000410e7c47b5.png)

 ![image](https://forum.radzen.com/uploads/default/original/1X/24348e0d3932d7942641a7f59037aa4580714e2b.png)

 ![image](https://forum.radzen.com/uploads/default/original/1X/51f0e02009268490a142ba5c432c159820afdaf4.png)

---

<div class="post-metadata">

**Author:** ![Werther](https://forum.radzen.com/user_avatar/forum.radzen.com/werther/32/12818_2.png) [@Werther](https://forum.radzen.com/u/Werther)\
**Post date:** [November 21, 2018, 6:37pm UTC](https://forum.radzen.com/t/uploading-files/616/5 "2018-11-21T18:37:35Z")

</div>

Do you guys have this functionality in the roadmap?

---

<div class="post-metadata">

**Author:** ![enchev](https://forum.radzen.com/user_avatar/forum.radzen.com/enchev/32/16_2.png) [@enchev](https://forum.radzen.com/u/enchev)\
**Post date:** [November 21, 2018, 7:59pm UTC](https://forum.radzen.com/t/uploading-files/616/6 "2018-11-21T19:59:07Z")

</div>

Products.ProductPicture in our Sample database is exactly varchar (MAX) and upload is working correctly.

---

<div class="post-metadata">

**Author:** ![Werther](https://forum.radzen.com/user_avatar/forum.radzen.com/werther/32/12818_2.png) [@Werther](https://forum.radzen.com/u/Werther)\
**Post date:** [November 21, 2018, 8:00pm UTC](https://forum.radzen.com/t/uploading-files/616/7 "2018-11-21T20:00:00Z")

</div>

I said varbinary(max) not varchar.

---

<div class="post-metadata">

**Author:** ![enchev](https://forum.radzen.com/user_avatar/forum.radzen.com/enchev/32/16_2.png) [@enchev](https://forum.radzen.com/u/enchev)\
**Post date:** [November 21, 2018, 8:01pm UTC](https://forum.radzen.com/t/uploading-files/616/8 "2018-11-21T20:01:54Z")

</div>

Hmm I see. At the moment only varchar is supported, we will research if we can add support for varbinary as well.

---

<div class="post-metadata">

**Author:** ![Werther](https://forum.radzen.com/user_avatar/forum.radzen.com/werther/32/12818_2.png) [@Werther](https://forum.radzen.com/u/Werther)\
**Post date:** [November 21, 2018, 8:03pm UTC](https://forum.radzen.com/t/uploading-files/616/9 "2018-11-21T20:03:01Z")

</div>

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![enchev](https://forum.radzen.com/user_avatar/forum.radzen.com/enchev/32/16_2.png) [@enchev](https://forum.radzen.com/u/enchev)\
**Post date:** [November 22, 2018, 8:31am UTC](https://forum.radzen.com/t/uploading-files/616/10 "2018-11-22T08:31:47Z")

</div>

You can [extend your model with not mapped property](https://www.radzen.com/documentation/extend-model-additional-property/) and use it to convert between string and binary:

```
  public partial class Product
  {
    [NotMapped]
    public string ProductPictureAsString
    {
      get
      {
        return System.Text.Encoding.Default.GetString(ProductPicture);
      }
      set
      {
        ProductPicture = System.Text.Encoding.Default.GetBytes(value);
      }
    }
  }

public partial class Startup
{
    partial void OnConfigureOData(ODataConventionModelBuilder builder)
    {
        // Expose additional property
        var product = builder.EntitySet<MyApp.Models.Test.Product>("Product");
        product.EntityType.Property(i => i.ProductPictureAsString);
    }
}

```

 ![image](https://forum.radzen.com/uploads/default/original/1X/a63f46099a25055b26b87451441e08886da31102.png)

 ![image](https://forum.radzen.com/uploads/default/original/1X/57946118afb2ca97256a8f8a6fd3ec2ad0e5673b.png)

Source code of the app can be found here:

> **[radzen-examples/UploadImageToBinary at master · radzenhq/radzen-examples](https://github.com/radzenhq/radzen-examples/tree/master/UploadImageToBinary)**
>
> Sample applications created with Radzen Studio. Contribute to radzenhq/radzen-examples development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![Werther](https://forum.radzen.com/user_avatar/forum.radzen.com/werther/32/12818_2.png) [@Werther](https://forum.radzen.com/u/Werther)\
**Post date:** [November 22, 2018, 11:00am UTC](https://forum.radzen.com/t/uploading-files/616/11 "2018-11-22T11:00:37Z")

</div>

Thanks korchev, I will test this as soon as possible.

---

<div class="post-metadata">

**Author:** ![enchev](https://forum.radzen.com/user_avatar/forum.radzen.com/enchev/32/16_2.png) [@enchev](https://forum.radzen.com/u/enchev)\
**Post date:** [November 22, 2018, 12:15pm UTC](https://forum.radzen.com/t/uploading-files/616/12 "2018-11-22T12:15:16Z")

</div>

I'm [enchev](http://forum.radzen.com/u/enchev) 🙂

---

<div class="post-metadata">

**Author:** ![Werther](https://forum.radzen.com/user_avatar/forum.radzen.com/werther/32/12818_2.png) [@Werther](https://forum.radzen.com/u/Werther)\
**Post date:** [November 22, 2018, 12:26pm UTC](https://forum.radzen.com/t/uploading-files/616/13 "2018-11-22T12:26:40Z")

</div>

Sorry!! Thanks enchev!

---

<div class="post-metadata">

**Author:** ![joshwilliam](https://forum.radzen.com/user_avatar/forum.radzen.com/joshwilliam/32/538_2.png) [@joshwilliam](https://forum.radzen.com/u/joshwilliam)\
**Post date:** [January 22, 2019, 5:23pm UTC](https://forum.radzen.com/t/uploading-files/616/14 "2019-01-22T17:23:09Z")

</div>

Finally made it back to this feature to uploading files. I can submit and store the files in the database with no problem. I still cannot get the files out. I did use the dataURL that was suggested but it doesn't do a thing.

This is what I used in the grid based on the suggestion from SeaderGamer but all I get is a blank page when I click on the link. -

`<a href="this.safeURL(item.Attachment)">download</a> `

 ![Capture](https://forum.radzen.com/uploads/default/original/1X/04e6af2e0f919496373eac4c6d5b142fca9375fd.png)

 ![Capture2](https://forum.radzen.com/uploads/default/original/1X/2deaabcd302e757da4d502a72613b63fef30e89b.png)

 ![image](https://forum.radzen.com/uploads/default/original/1X/4cd4969d0b1a73891f62031a47e07d0a45b853a1.png)

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [January 22, 2019, 7:49pm UTC](https://forum.radzen.com/t/uploading-files/616/15 "2019-01-22T19:49:21Z")

</div>

I don't think this will work as there isn't any `safeURL` method by default and also the current data item is available as `${data}` instead of `item`.

Try the following instead:

1. Create a page property with **Name** `getExtension` and **Value** :

```auto
(dataURI) => {
  return dataURI.substring(dataURI.indexOf('/') + 1, dataURI.indexOf(';base64'));
}

```

It will determine the attachment extension based on its content type (e.g. png, jpg, pdf etc).
2. Change the **Template** of the column to  
`<a href="${data.Attachment}" download="Attachment.${getExtension(data.Attachment)}">download</a>`

Here is what this does:

1. The first step creates a function that determines the type of the file - we need it to set the proper extension when the user tries to download it. Radzen stores files in a database as [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs). A data URL begins with the type of the file e.g. `image/png`, `text/html` etc.
2. We set the **Template** of the column in way to allow modern browsers to save a file.

---

<div class="post-metadata">

**Author:** ![joshwilliam](https://forum.radzen.com/user_avatar/forum.radzen.com/joshwilliam/32/538_2.png) [@joshwilliam](https://forum.radzen.com/u/joshwilliam)\
**Post date:** [January 22, 2019, 8:28pm UTC](https://forum.radzen.com/t/uploading-files/616/16 "2019-01-22T20:28:00Z")

</div>

This works for downloading images but not for other file types such as Excel (xlsx) or PDF's. It is also not keeping the filename when I am downloading it. It saved the image as Attachment.png

---

<div class="post-metadata">

**Author:** ![joshwilliam](https://forum.radzen.com/user_avatar/forum.radzen.com/joshwilliam/32/538_2.png) [@joshwilliam](https://forum.radzen.com/u/joshwilliam)\
**Post date:** [January 22, 2019, 8:36pm UTC](https://forum.radzen.com/t/uploading-files/616/17 "2019-01-22T20:36:16Z")

</div>

Also after testing this in Internet Explorer as well, the links do not work at all even for the Image attachment.

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [January 22, 2019, 9:06pm UTC](https://forum.radzen.com/t/uploading-files/616/18 "2019-01-22T21:06:24Z")

</div>

Radzen doesn't keep the filename in the database during uploads - only the data. If you want to persist the original filename you will need to upload the files with your own code and persist the file name in a separate column of your database (Radzen doesn't do that by default).

For other file types (XLSX, PDF) you will need to change slightly the column template to

`<a [href]="data.Attachment | safe" download="Attachment.${getExtension(data.Attachment)}">download</a>`

Indeed older browser versions don't support the `download` attribute of the `<a>` element and will need additional code. I will have to research further how to support that.

---

<div class="post-metadata">

**Author:** ![joshwilliam](https://forum.radzen.com/user_avatar/forum.radzen.com/joshwilliam/32/538_2.png) [@joshwilliam](https://forum.radzen.com/u/joshwilliam)\
**Post date:** [January 22, 2019, 9:16pm UTC](https://forum.radzen.com/t/uploading-files/616/19 "2019-01-22T21:16:41Z")

</div>

So close I can taste it. Images and PDF now work but Excel does not. The extension type is recorded as .sheet

Attachment.vnd.openxmlformats-officedocument.spreadsheetml.sheet

I had already implemented saving my file names and sizes in the database using the new select property you guys recently implemented. I saved the filenames in the database as a field called fileName. Honestly the name is just a nice to have, although I'm sure my users won't think the same lol.

Thanks again,

Josh

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [January 22, 2019, 9:37pm UTC](https://forum.radzen.com/t/uploading-files/616/20 "2019-01-22T21:37:22Z")

</div>

If you already store the filename you can just use it 🙂

`<a [href]="data.Attachment | safe" download="${data.FileName}">download</a>`

Just replace ${data.FileName} with the name of the column you use to store the filename. Then you don't need the `getExtension` property (which can't seem to correctly determine the extension for excel files).

[Next page](https://forum.radzen.com/t/uploading-files/616.md?page=2)
