Vb Web Browser Download File From Click
- Updated date Jan 14, 2021
- 132k
- vi
In this article, you will learn most file upload, view and download in ASP.Net.
In this article, you will come to know almost the following things:
- How to upload a file with an instance epitome file .
- How to open up an uploaded file in the browser to read.
- How to download an uploaded file.
In many scenarios, we have to develop, upload, and open the uploaded file in the Browser and download an uploaded file.
Upload a File : (NewFriend.aspx)
In this folio, nosotros will have and insert a new friend with the image.
Friend Listing: (FriendList.aspx)This page will display the friends with the images. In that location are two buttons, View and Download.
- View Button : Click this button and it volition redirect you on ViewImage.aspx
- Download Button : Click this push button and the image volition download.
File UploadNosotros can upload and receive the file from the user with the aid of FileUpload control. Nosotros can receive/ let the upload with the following points:
- Check size of the file. Nosotros can restrict the size of the file to be uploaded.
- Extension of the file. We tin can restrict the extension of file to be uploaded.
While we are accepting the file from the user through the file upload, always feed extra information in the file name similar: DateTime , UserID or Friend ID etc..
When Web master or site administrator volition view the image folder, here, nosotros will only upload the paradigm and View on new tab of the Browser and download the same.
Lawmaking To Display Image in Friend List page : FriendList.aspx
- < asp:TemplateField HeaderText = "Photo" >
- < ItemTemplate >
- < asp:Image ID = "imgPicture" runat = "server" ImageUrl = '<%# Eval("FriendImage", "~/FriendPhoto/{0}") %>' Width = "200px" />
- </ ItemTemplate >
- </ asp:TemplateField >
Code To Display Image on Another Tab: ViewImage.aspx
- protected void btnView_Click( object sender, EventArgs east)
- {
- LinkButton lnkbtn = senderevery bit LinkButton;
- GridViewRow gvrow = lnkbtn.NamingContaineras GridViewRow;
- string filePath = "~//FriendPhoto//" + gvFriend.DataKeys[gvrow.RowIndex].Value.ToString();
- Response.Write(String.Format("<script>window.open('{0}','_blank');</script>" , "viewImage.aspx?fn=" + filePath));
- }
In GridView, we are having a push called View:
Code To Download a selected Image
- protected void btnDownload_Click( object sender, EventArgs eastward)
- {
- Button btn = senderas Push;
- GridViewRow gvrow = btn.NamingContaineras GridViewRow;
- string filePath = "FriendPhoto\\" +gvFriend.DataKeys[gvrow.RowIndex].Value.ToString();
- Response.ContentType ="paradigm/jpg" ;
- Response.AddHeader("Content-Disposition" , "zipper;filename=\"" + filePath + "\"" );
- Response.TransmitFile(Server.MapPath(filePath));
- Response.End();
- }
Footstep by Stride for achieving our job
- Create Project
Create a new ASP.NET Empty Web Site projection named: FileUploadAndViewAndDownload, - Add three Web Forms named as following:
Spider web Form File Proper noun Descripton FriendList.aspx To display all our friends in the grid view. NewFriend.aspx To insert a new friend in database table called tblFileUpload. ViewImage.aspx To display a new friend in the new Browser tab.
- Add together new Folder
Create a new binder called FRIENDPHOTO. - Web.Config
Update your webconfig file with your SQL Server settings.
- < connectionStrings >
- < add name = "fileuploadConnectionString" connectionString = "Data Source=192.168.1.50\sa;Initial Catalog=mbktest;Persist Security Info=True;User ID=sa;Password=clserver" providerName = "System.Information.SqlClient" />
- </ connectionStrings >
Lawmaking FriendList.aspx
- < %@ Folio Language = "C#" AutoEventWireup = "true" CodeFile = "FriendList.aspx.cs" Inherits = "FriendList" EnableEventValidation = "true" % >
- <!DOCTYPE html>
- < html xmlns = "http://www.w3.org/1999/xhtml" >
- < head runat = "server" >
- < title > </ title >
- </ head >
- < body >
- < form id = "form1" runat = "server" >
- < div >
- < a href = "NewFriend.aspx" > Insert An New Friend </ a >
- < asp:GridView ID = "gvFriend" DataKeyNames = "FriendImage" runat = "server" AutoGenerateColumns = "faux" BackColor = "#DEBA84" BorderColor = "#DEBA84" BorderStyle = "None" BorderWidth = "1px" CellPadding = "3" CellSpacing = "ii" >
- < FooterStyle BackColor = "#F7DFB5" ForeColor = "#8C4510" />
- < HeaderStyle BackColor = "#A55129" Font-Bold = "True" ForeColor = "White" />
- < PagerStyle ForeColor = "#8C4510" HorizontalAlign = "Heart" />
- < RowStyle BackColor = "#FFF7E7" ForeColor = "#8C4510" />
- < SelectedRowStyle BackColor = "#738A9C" Font-Assuming = "True" ForeColor = "White" />
- < SortedAscendingCellStyle BackColor = "#FFF1D4" />
- < SortedAscendingHeaderStyle BackColor = "#B95C30" />
- < SortedDescendingCellStyle BackColor = "#F1E5CE" />
- < SortedDescendingHeaderStyle BackColor = "#93451F" />
- < Columns >
- < asp:BoundField DataField = "FriendID" HeaderText = "Friend ID" />
- < asp:BoundField DataField = "Proper noun" HeaderText = "Friend Name" />
- < asp:BoundField DataField = "Place" HeaderText = "Place" />
- < asp:BoundField DataField = "Mobile" HeaderText = "Mobile Number" />
- < asp:TemplateField HeaderText = "Photo" >
- < ItemTemplate >
- < asp:Image ID = "imgPicture" runat = "server" ImageUrl = '<%# Eval("FriendImage", "~/FriendPhoto/{0}") %>' Width = "200px" />
- </ ItemTemplate >
- </ asp:TemplateField >
- < asp:TemplateField HeaderText = "View" >
- < ItemTemplate >
- < asp:LinkButton ID = "btnView" runat = "server" Text = "View" OnClick = "btnView_Click" />
- </ ItemTemplate >
- </ asp:TemplateField >
- < asp:TemplateField HeaderText = "Download" >
- < ItemTemplate >
- < asp:Button ID = "btnDownload" runat = "server" Text = "Download" OnClick = "btnDownload_Click" />
- </ ItemTemplate >
- </ asp:TemplateField >
- </ Columns >
- </ asp:GridView >
- </ div >
- </ form >
- </ torso >
- </ html >
Code FriendList.aspx .cs
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using Organisation.Spider web;
- using System.Spider web.UI;
- using System.Web.UI.WebControls;
- public fractional form FriendList : Organisation.Web.UI.Folio
- {
- protected void Page_Load( object sender, EventArgs eastward)
- {
- if (!IsPostBack)
- {
- BindGridView();
- }
- }
- private void BindGridView()
- {
- string constr = ConfigurationManager.ConnectionStrings[ "fileuploadConnectionString" ].ConnectionString;
- SqlConnection con =new SqlConnection(constr);
- SqlDataAdapter da =new SqlDataAdapter( "Select * From tblFileUpload" , con);
- DataSet ds =new DataSet();
- da.Fill(ds,"Friend" );
- gvFriend.DataSource = ds;
- gvFriend.DataBind();
- }
- protected void btnView_Click( object sender, EventArgs e)
- {
- LinkButton lnkbtn = senderas LinkButton;
- GridViewRow gvrow = lnkbtn.NamingContaineras GridViewRow;
- string filePath = "~//FriendPhoto//" + gvFriend.DataKeys[gvrow.RowIndex].Value.ToString();
- Response.Write(String.Format("<script>window.open up('{0}','_blank');</script>" , "viewImage.aspx?fn=" + filePath));
- }
- protected void btnDownload_Click( object sender, EventArgs e)
- {
- Button btn = senderas Button;
- GridViewRow gvrow = btn.NamingContaineras GridViewRow;
- string filePath = "FriendPhoto\\" +gvFriend.DataKeys[gvrow.RowIndex].Value.ToString();
- Response.ContentType ="image/jpg" ;
- Response.AddHeader("Content-Disposition" , "attachment;filename=\"" + filePath + "\"" );
- Response.TransmitFile(Server.MapPath(filePath));
- Response.Finish();
- }
- }
Code NewFriend.aspx
- < %@ Page Language = "C#" AutoEventWireup = "true" CodeFile = "NewFriend.aspx.cs" Inherits = "NewFriend" % >
- <!DOCTYPE html>
- < html xmlns = "http://www.w3.org/1999/xhtml" >
- < head runat = "server" >
- < title > </ championship >
- </ head >
- < torso >
- < form id = "form1" runat = "server" >
- < div >
- < h1 > Friend Detail </ h1 >
- < table >
- < tr >
- < td >
- Name
- </ td >
- < td >
- < asp:TextBox ID = "txtName" runat = "server" > </ asp:TextBox >
- </ td >
- </ tr >
- < tr >
- < td >
- Place
- </ td >
- < td >
- < asp:TextBox ID = "txtPlace" runat = "server" > </ asp:TextBox >
- </ td >
- </ tr >
- < tr >
- < td >
- Mobile
- </ td >
- < td >
- < asp:TextBox ID = "txtMobile" runat = "server" > </ asp:TextBox >
- </ td >
- </ tr >
- < tr >
- < td >
- Photograph/Image
- </ td >
- < td >
- < asp:FileUpload ID = "fuImage" runat = "server" take = ".png,.PNG,.bmp,.BMP,.jpeg,.JPEG,.jpg,.JPG" > </ asp:FileUpload >
- </ td >
- </ tr >
- < tr >
- < td >
- < asp:Button ID = "btnSubmit" runat = "server" Text = "Submit" OnClick = "btnSubmit_Click" />
- </ td >
- < td >
- < asp:Push button ID = "btnBack" runat = "server" Text = "Back" OnClick = "btnBack_Click" PostBackUrl = "~/FriendList.aspx" />
- </ td >
- </ tr >
- </ table >
- </ div >
- </ grade >
- </ body >
- </ html >
Code NewFriend.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Arrangement.Web;
- using System.Spider web.UI;
- using System.Web.UI.WebControls;
- using Organisation.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- using Arrangement.IO;
- public fractional class NewFriend : System.Web.UI.Page
- {
- protected void Page_Load( object sender, EventArgs east)
- {
- }
- protected void btnSubmit_Click( object sender, EventArgs due east)
- {
- string filename = Path.GetFileName(fuImage.PostedFile.FileName);
- cord str = DateTime.Now.ToString( "hhmmssffffff" )+filename;
- fuImage.SaveAs(Server.MapPath("FriendPhoto/" +str));
- string constr = ConfigurationManager.ConnectionStrings[ "fileuploadConnectionString" ].ConnectionString;
- SqlConnection con =new SqlConnection(constr);
- con.Open();
- SqlCommand cmd =new SqlCommand( "Insert into tblFileUpload (Proper name,Place,Mobile, FriendImage) Values(@Name,@Place,@Mobile,@FriendImage)" , con);
- cmd.Parameters.AddWithValue("@Proper noun" ,txtName.Text);
- cmd.Parameters.AddWithValue("@Place" ,txtPlace.Text);
- cmd.Parameters.AddWithValue("@Mobile" , txtMobile.Text);
- cmd.Parameters.AddWithValue("@FriendImage" , str);
- cmd.ExecuteNonQuery();
- cmd.Dispose();
- con.Shut();
- Response.Redirect("~/FriendList.aspx" );
- }
- }
Code ViewImage.aspx
- < %@ Folio Language = "C#" AutoEventWireup = "truthful" CodeFile = "viewImage.aspx.cs" Inherits = "viewImage" % >
- <!DOCTYPE html>
- < html xmlns = "http://www.w3.org/1999/xhtml" >
- < head runat = "server" >
- < championship > </ title >
- </ head >
- < trunk >
- < form id = "form1" runat = "server" >
- < div >
- < asp:Image ID = "Image1" runat = "server" Height = "408px" Width = "649px" />
- </ div >
- </ course >
- </ body >
- </ html >
Code ViewImage.aspx.cs
- using System;
- using Organization.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Web;
- using Organization.Spider web.UI;
- using Organization.Spider web.UI.WebControls;
- public partial class viewImage : System.Web.UI.Page
- {
- protected void Page_Load( object sender, EventArgs e)
- {
- cord FilePath = goose egg ;
- FilePath = Asking.QueryString["fn" ];
- if (FilePath != null )
- {
- cord path = Server.MapPath(FilePath);
- Image1.ImageUrl = FilePath;
- }
- }
- }
Output
DOWNLOAD HERE
Posted by: kaplanyeasught.blogspot.com
Post a Comment