Sending File to Clients
-
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
-
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
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..
-
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..
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
-
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
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..
-
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..
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