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.

9
  • You really have the needed usings declared ? Commented May 15, 2016 at 14:52
  • I'm currently using System.Web.Mvc Commented May 15, 2016 at 14:56
  • can you show how you declare your controller class ? Commented May 15, 2016 at 14:56
  • Can you try it with return Sytem.Web.Mvc.File(...) no difference ? Commented May 15, 2016 at 15:01
  • @Shyju I've added the class. Commented May 15, 2016 at 15:01

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I up-voted your answer as you were able to submit it just before I submitted my answer.
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;
        }
    }
}

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.