0

I am trying to download a folder that is on my drive C:/. My program doesn't show any error, it just doesn't work. I don't know what am I doing wrong, I think this folder name could be wrong? Or code? This is my code :

        [HttpPost]

    public HttpResponseMessage DownloadFile(DownloadInput input)
    {
        if (!string.IsNullOrEmpty(input.DosyaAdi))
        {
            string filePath = "C:/Uploads";
            string fullPath = filePath + "/" + input.FileID + "/" + input.FileNm;
            return CreateResponseFileContent(fullPath);
            //string fullPath = AppDomain.CurrentDomain.BaseDirectory + filePath + "/" + input.SorunID + "/" + input.DosyaAdi;
        }
        return new HttpResponseMessage(HttpStatusCode.NotFound);
    }

    public HttpResponseMessage CreateResponseFileContent(string fullPath)
    {
        HttpResponseMessage result = null;
        try
        {
            result = Request.CreateResponse(HttpStatusCode.OK);
            var fileStream = new FileStream(fullPath, FileMode.Open);
            result.Content = new StreamContent(fileStream);
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            result.Content.Headers.ContentDisposition.FileName = fullPath;
            return result;
        }
        catch (Exception ex)
        {
            return Request.CreateResponse(HttpStatusCode.Gone);
        }
    }
3
  • 1
    Does this answer your question? Returning a file to View/Download in ASP.NET MVC Commented Dec 13, 2019 at 13:16
  • Also if you want to only get something from the server (or your drive C in this case), use HttpGet, if you want to send something to the server, use HttpPost. Explained better here Commented Dec 13, 2019 at 13:21
  • I think this solution fine , ı try now thank you for help Commented Dec 13, 2019 at 13:25

1 Answer 1

1

Why you don't use File action result?

public FileResult DownloadFile()
{
    ...
    return File(file, document.ContentType);
}
Sign up to request clarification or add additional context in comments.

3 Comments

I am trying download in my folder not database, is this code for my problem??
@YilmazFurkan I didn't use any database related concept, It is a normal variable with name "file" which contains your file path.
Okey I understand

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.