0

I have saved some files in SQL table through an application in admin side, but now I have to take back the files saved in the table to a folder in another application to the website. I need to save the files in a folder called MyFiles and want to download the files by clicking on the download link controller

    public ActionResult Index()
    {
        var Downloads = db.Downloads.ToList();
        return View(Downloads);
    }

View

@model List<ThaniyamBank.Models.Download>


<div class="row">

        @foreach (var item in Model)
        {
            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-12 form_cont">
                <p class="all_sub_hed">@item.ItemName</p>
                <p class="tet">@item.Description</p>
                <a href="http://localhost:56572/MyFiles/@(item.Url)" class="hvr-icon-back">Download Form</a>
            </div>
        }
    </div>

this is my folder which I need to save the data from table

enter image description here

the files shows here is which I have added directly

How can I get files directly from table

this is my table called downloads

enter image description here

Here the Url column is the name of the file with extension, I need to get this Url to the folder myFiles in the application. How can I get the files from table? can anyone please help me to find the solution, how the working ??

4
  • Use Server.MapPath with directory path where the files saved into and file name taken from DB (assumed EF is being used, you can query by BankEntities.Downloads.Where(...).Select(x => x.Url), e.g. Server.MapPath("~/MyFiles" + model.Url). Commented Mar 10, 2017 at 4:44
  • But Iam not passing id in action parameter to give where clause . how can it works properly ? Commented Mar 10, 2017 at 5:10
  • In table you just have file name. So where is actually file exist? Does it exist in admin project and you want to save it in another your main website project? Commented Mar 10, 2017 at 6:11
  • yes. admin ads the file and it also want to save it folder of another application, which is used by users. But I don't know how to save this in the folder Commented Mar 10, 2017 at 6:17

2 Answers 2

1

You should define your file path by mapping to its server path. In your action, you can do like this :

public ActionResult Index()
    {
        var Downloads = db.Downloads.ToList();
        Downloads.ForEach(x=>{
           x.Url =  Server.MapPath(Url.Content("~/MyFiles/" + x.Url));
        });
        return View(Downloads);
    }

In your view side, you can simply give Url :

@model List<ThaniyamBank.Models.Download>
<div class="row">

        @foreach (var item in Model)
        {
            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-12 form_cont">
                <p class="all_sub_hed">@item.ItemName</p>
                <p class="tet">@item.Description</p>
                <a href="@(item.Url)" class="hvr-icon-back">Download Form</a>
            </div>
        }
    </div>
Sign up to request clarification or add additional context in comments.

18 Comments

while giving is code in action, getting error like List<Download> does not contain definition foe Foreach and no extension method accepting first argument of type 'List<Download>' could be found, why is this error ??
you need to import System.Linq by using System.Linq;
but it is not downloading and no error generating. I checked using breakpoints and noticed that the Url shows the entire path . I need only the Name of the file ?When I ckilck on the download lik it is not downloading. why ?
yeah, it returns complete url path so you don't need to specify full url in view section. you can just do : <a href="@(item.Url)" class="hvr-icon-back">Download Form</a>
Iam getting the path But it is not downloading by clicking the link and not saving the data from table to the folder ??
|
0

First thing, you need to change URL Column of your Table. It should contains full Directory path of your PDF file at your Admin Project and add another column for file name.

And in your Action Method, Simply retrieve the data from your table and pass it to view.

public ActionResult Index()
{
   var Downloads = db.Downloads.ToList();
   return View(Downloads);
}

And at your View, bind File URL to another action link:

@model List<ThaniyamBank.Models.Download>
<div class="row">

@foreach (var item in Model)
    {
        <div class="col-lg-8 col-md-8 col-sm-8 col-xs-12 form_cont">
            <p class="all_sub_hed">@item.ItemName</p>
            <p class="tet">@item.Description</p>
            <a href="@@Url.Action("SaveFile", "Download"), new { FilePath = @item.Url, FileName = @item.FileName })" class="hvr-icon-back">Download Form</a>
        </div>
    }
</div>

And in your SaveFile Action Method:

public ActionResult SaveFile(string FilePath, string FileName)
{
     using(WebClient client = new WebClient())
     {
        client.DownloadFile(FilePath , Server.MapPath(Path.Combine("~/MyFiles/", FileName));
     }
}

WebClient

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.