0

In my website I have listed the ftp folder file, when user click the download button i call the code below, then download the file to their c drive. However I getting error Access to the path 'C:\myvideo.flv' is denied.

Code is running on server, and needed to save to local PC drive.

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via , the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

Did I miss sometime in my FtpWebRequest or other cause this issue.

      FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FTPRequest_Host + "myvideo.flv");
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.KeepAlive = true;
        request.UsePassive = true;
        request.UseBinary = true;

        request.Credentials = new NetworkCredential(FTPRequest_Username, FTPRequest_Password);

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);

        using (FileStream writer = new FileStream(@"C: \\" + "myvideo.flv", FileMode.Create, FileAccess.ReadWrite))
        {

            long length = response.ContentLength;
            int bufferSize = 2048;
            int readCount;
            byte[] buffer = new byte[2048];

            readCount = responseStream.Read(buffer, 0, bufferSize);
            while (readCount > 0)
            {
                writer.Write(buffer, 0, readCount);
                readCount = responseStream.Read(buffer, 0, bufferSize);
            }
        }

        reader.Close();
        response.Close();
FeelRightz
  • 2,777
  • 2
  • 38
  • 73
  • Are you running your application as an Administrator? Can you place a file directly in C:\ as the user you're running as? Without any prompts from Windows? – ProgrammingLlama May 04 '18 at 04:41
  • 1
    You do have a weird space after `C:` and two `\\` characters. I'm not sure if this is the source of the problem or it's something else... – lc. May 04 '18 at 04:41
  • The correct local path should be `@"C:\myvideo.flv"` because it's enclosed in literal string. Also the `C:` drive considered system drive in Windows 7 or later, you need elevated privileges (i.e. running VS as administrator) to save in `C:` drive. – Tetsuya Yamamoto May 04 '18 at 04:46

4 Answers4

2

It's permission issue. If you are running your application in Visual Studio (IIS Express), open Visual Studio with Run as administrator. If it's hosted in IIS, Application Pool Identity should have sufficient permission to access C drive.

Sudipta Kumar Maiti
  • 1,669
  • 1
  • 12
  • 18
1

I think you may have permission-related issue regarding usage of C:\ drive, because in Windows Vista and later versions that drive treated as system drive, hence you will need running the code by user with elevated privileges to save video file in corresponding drive.

If you want to avoid any mess with file permissions, try changing the path of video file to save by using Server.MapPath(), because ASP.NET by default doesn't let direct access to the root of server's drive:

using (FileStream writer = new FileStream(Server.MapPath("~/files/myvideo.flv", FileMode.Create, FileAccess.ReadWrite))
{
    // other stuff
}

If you still want to save video file in system drive using provided code, run your application pool with user that has administrator privileges in application pool identity.

Additional info:

Since you're running request process in ASP.NET environment, consider to give write permission for current application pool identity user with specified file/folder path.

Similar issue:

FileStream not letting me create file on C drive

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • Hi, I get this error `ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via , the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. ` – FeelRightz May 04 '18 at 04:59
  • Well, I realized that ASP.NET doesn't let access to root path of any drives in server, edited with proper usage of `FileStream` in controller action. – Tetsuya Yamamoto May 04 '18 at 05:10
  • I want to save to local drive not server drive, but code is running on server not localhost – FeelRightz May 04 '18 at 05:20
  • "local drive" => are you mean client machine's drive? If I'm right, nope, neither you have control where the user save the file in client-side, nor opening saved file inside client's machine. If you want to play the stream in client browser, use `return File()` with `object` or `embed` tag instead. – Tetsuya Yamamoto May 04 '18 at 05:43
  • in my website i have listed the ftp folder file, when user click the download button i call the code of my (in the question), then download the file to their c drive, so this is impossible ? – FeelRightz May 04 '18 at 05:47
  • Specifying where user will store downloaded file & which player should play it in client's machine from server-side are impossible. The applicable way is returning downloaded stream from FTP with `FileResult` to client's browser & let the client decide to save that file, or embed the stream in view page with third-party JS library. – Tetsuya Yamamoto May 04 '18 at 05:56
  • which mean I have to download the file first to server drive then only can let user download directly from the server? – FeelRightz May 04 '18 at 05:58
  • Right, you need to store the file in server first, then let the user download it by providing link to action method or stream which returns either `FileResult` or `FileStreamResult`, because you don't have any control to client's machine behavior. – Tetsuya Yamamoto May 04 '18 at 06:08
0

Problem is not in FtpWebRequest. That exception is coming from FileStream object, because probably you have opened video file in some software (e.g video player) that prevents FileStream object to write in that file.

Dzliera
  • 646
  • 5
  • 15
0

you are getting confused with what things do

@"C: \\" + "myvideo.flv"

the @ means you don't have to escape backslahes, so we get

@"C: \" + "myvideo.flv"

but there is a weird space

@"C:\" + "myvideo.flv"

So this is pretty ok ( assuming you can write to the root of your C:\ )

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156