Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Sending File to Clients

Sending File to Clients

Scheduled Pinned Locked Moved ASP.NET
security
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Samy Lahur
    wrote on last edited by
    #1

    Dear All, I have some files stored in a folder except wwwroot and I need a serverside code to read that file and send it to the client after checking some user authentication and authorization for download. Please share your code samples. Regards, Samy

    M 1 Reply Last reply
    0
    • S Samy Lahur

      Dear All, I have some files stored in a folder except wwwroot and I need a serverside code to read that file and send it to the client after checking some user authentication and authorization for download. Please share your code samples. Regards, Samy

      M Offline
      M Offline
      MihirV
      wrote on last edited by
      #2

      hi, *ASPNET machine user must have access rights to this file.

      string sFilePath = Server.MapPath( [your file name] );
      
      if ( File.Exists( sFilePath ) )
      {
      Response.Clear();					
      Response.ContentType = "application/" + Path.GetExtension( sFilePath );
      Response.AddHeader( "Content-Disposition", "attachment; filename=\"" +    Path.GetFileName( sFilePath ) + "\"" );
      Response.Flush();
      Response.WriteFile( sFilePath );
      Response.End();
      }
      

      :) Confidence comes not from always being right, but from not fearing to be wrong..... Mihir..

      S 1 Reply Last reply
      0
      • M MihirV

        hi, *ASPNET machine user must have access rights to this file.

        string sFilePath = Server.MapPath( [your file name] );
        
        if ( File.Exists( sFilePath ) )
        {
        Response.Clear();					
        Response.ContentType = "application/" + Path.GetExtension( sFilePath );
        Response.AddHeader( "Content-Disposition", "attachment; filename=\"" +    Path.GetFileName( sFilePath ) + "\"" );
        Response.Flush();
        Response.WriteFile( sFilePath );
        Response.End();
        }
        

        :) Confidence comes not from always being right, but from not fearing to be wrong..... Mihir..

        S Offline
        S Offline
        Samy Lahur
        wrote on last edited by
        #3

        Dear Mihir, The Server.MapPath Requires a Virtual path on the server, But my file is not web shared and is stored in a place which is not accessible through web. I stored my files in e:\inetpub\Files and the web server root is e:\inetpub\wwwroot. Therefore I want to access the files e:\inetpub\file\file1.zip and send it to client. The Server.MapPath("file1.zip") returns e:\inetpub\wwwroot\file1.zip which is not a valid path and therefore the file exist check is false. Do you have any idea? Samy

        M 1 Reply Last reply
        0
        • S Samy Lahur

          Dear Mihir, The Server.MapPath Requires a Virtual path on the server, But my file is not web shared and is stored in a place which is not accessible through web. I stored my files in e:\inetpub\Files and the web server root is e:\inetpub\wwwroot. Therefore I want to access the files e:\inetpub\file\file1.zip and send it to client. The Server.MapPath("file1.zip") returns e:\inetpub\wwwroot\file1.zip which is not a valid path and therefore the file exist check is false. Do you have any idea? Samy

          M Offline
          M Offline
          MihirV
          wrote on last edited by
          #4

          hi samy, you can replace the file path with yours but make sure that your folder must have rights(atleast Read) to the ASPNET machine user.

          string sFilePath = "c:\\inetpub\\wwwroot\\files\\file1.ext";
          

          it will work fine...:) Confidence comes not from always being right, but from not fearing to be wrong..... Mihir..

          S 1 Reply Last reply
          0
          • M MihirV

            hi samy, you can replace the file path with yours but make sure that your folder must have rights(atleast Read) to the ASPNET machine user.

            string sFilePath = "c:\\inetpub\\wwwroot\\files\\file1.ext";
            

            it will work fine...:) Confidence comes not from always being right, but from not fearing to be wrong..... Mihir..

            S Offline
            S Offline
            Samy Lahur
            wrote on last edited by
            #5

            Dear Mihir, You are right. I checked the Response.WriteFile with a path and also sent the Header and MIME type and it worked perfect. Thanks a lot, Samy

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups