1

I'm currently writing a simple controller method in my MVC WEB Api application which downloads file from a Dropbox account. The problem is I can't return the file from the method: it says "The name 'File' does not exist in the current context", but in the documentation this constructor can be called.
Code:

public async Task<FileResult> GetFile(string folder, string file)
{
    using (var dbx = new DropboxClient("generated token key"))
    {
        using (var response = await dbx.Files.DownloadAsync(folder + "/" + file))
        {
            var result = await response.GetContentAsStringAsync();
            return File(result, file);
        }
    }
}

Complete DropBoxController.cs class:

using Dropbox.Api;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Mvc;

namespace TestDropBox.Controllers
{
    public class DropBoxController : ApiController
    {
        // GET: api/DropBox
        public async Task<IEnumerable<string>> Get()
        {

            List<string> AccountData= new List<string>();
            using (var dbx = new DropboxClient("generated token"))
            {
                var full = await dbx.Users.GetCurrentAccountAsync();
                Console.WriteLine("{0} - {1}", full.Name.DisplayName, full.Email);

                AccountData.Add(full.Name.DisplayName);
                AccountData.Add(full.Email);

            }

            return DatiAccount;
        }

        [System.Web.Http.HttpGet]
        public async Task<FileResult> GetFile(string folder, string file)
        {
            using (var dbx = new DropboxClient("generated token"))
            {
                using (var response = await dbx.Files.DownloadAsync(folder + "/" + file))
                {
                    var result = await response.GetContentAsStringAsync();
                    return new File(result, file);
                }
            }
        }


        // GET: api/DropBox/5
        public string Get(int id)
        {
            return "value";
        }

        // POST: api/DropBox
        public void Post([FromBody]string value)
        {
        }

        // PUT: api/DropBox/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/DropBox/5
        public void Delete(int id)
        {
        }
    }
}

How could I fix this problem? Thank you.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Gab
  • 87
  • 3
  • 7

2 Answers2

2

You inherit from ApiController, so the System.Web.Mvc.File ctor isn't accessible (is protected), that's why you recive the error.

Have a look at this thread: File Download with api controller

Community
  • 1
  • 1
Cyril Iselin
  • 596
  • 4
  • 20
  • I up-voted your answer as you were able to submit it just before I submitted my answer. – Nkosi May 15 '16 at 15:27
1

You are using ApiController. Your updated method could look something like...

[System.Web.Http.HttpGet]
public async Task<HttpResponseMessage> GetFile(string folder, string fileName) {
    using (var dbx = new DropboxClient("generated token")) {
        using (var file = await dbx.Files.DownloadAsync(folder + "/" + fileName)) {
            var content = await file.GetContentAsStringAsync();
            var statuscode = HttpStatusCode.OK;
            var response = Request.CreateResponse(statuscode);
            var stream = new MemoryStream(Encoding.UTF8.GetBytes(content ?? ""));
            response.Content = new StreamContent(stream);
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {
                FileName = fileName
            };
            return response;
        }
    }
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472