0

I'm working on an assignment, and I'm stuck in downloading section. I can upload the file. But when I'm trying to download the same file after uploading, I get an error ("Access is denied").

Here is the code which I have written:

[HttpGet]
public FileResult Download (string fileName)
{
    var permissionSet = new PermissionSet(PermissionState.None);

    var writePermission = new FileIOPermission(FileIOPermissionAccess.Write, @"D:\Assignment\Ment\Ment\Photos");
    permissionSet.AddPermission(writePermission);

    var FileVirtualPath = Server.MapPath("~/Photos/" + fileName); //"~/Photos/" + fileName;

    return new FilePathResult(@"D:\Assignment\Ment\Ment\Photos", "application/octet-stream");
}
3
  • You use MapPath and then don’t use that for actually returning a file, rather suddenly return a result to a folder. Create permissionSet and don’t use it for anything. Commented Oct 14, 2017 at 4:05
  • Can you edit this code? Commented Oct 14, 2017 at 4:10
  • I Changed the code to this, But still not working var FileVirtualPath = ("~/Photos/" + fileName); return File(FileVirtualPath,"application/force-download", Path.GetFileName(FileVirtualPath)); Commented Oct 14, 2017 at 4:43

2 Answers 2

2

You could do

[HttpGet]
    public virtual ActionResult Download(string fileName)
    {
        //fileName should be like "photo.jpg"
        string fullPath = Path.Combine(Server.MapPath("~/Photos"),fileName);
        return File(fullPath, "application/octet-stream", fileName);
    }
Sign up to request clarification or add additional context in comments.

6 Comments

I changed the code to this, And now i am getting error ............. Exception Details: System.IO.FileNotFoundException: Could not find file 'D:\Work\Ment\Ment\Photos\photo.jpg'.
I tried adding this code in my controller in asp net core mvc project but cant figure out which import I need for Server
@SteveW should be under System.Web, make sure you reference it in your project
I tried adding using System.Web but it doesn't work. I am trying to do this inside my MVC controller action method. Does this make a difference? I will make a new question and give you the link if you don't mind helping
|
2

Try something like that :

public FileResult Download(string ImageName)
{
    return File(“<your path>” + ImageName, System.Net.Mime.MediaTypeNames.Application.Octet);
}

Also, visit this links :

ASP.NET MVC Uploading and Downloading Files

Upload and download files using ASP.NET MVC

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.