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);
        }
    }
Yilmaz Furkan
  • 21
  • 1
  • 6
  • 1
    Does this answer your question? [Returning a file to View/Download in ASP.NET MVC](https://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-asp-net-mvc) – Michał Szymański Dec 13 '19 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](https://restfulapi.net/http-methods/) – Michał Szymański Dec 13 '19 at 13:21
  • I think this solution fine , ı try now thank you for help – Yilmaz Furkan Dec 13 '19 at 13:25

1 Answers1

1

Why you don't use File action result?

public FileResult DownloadFile()
{
    ...
    return File(file, document.ContentType);
}
Saeid Babaei
  • 481
  • 2
  • 16