Open file dialog in asp.net?

Posted by Oswaldlily under ASP.NET on 4/26/2013 | Points: 10 | Views : 31139 | Status : [Member] | Replies : 5
I have a link ..when I click on that should get "openfiledialog"
where i need to select my .txt file..
How should i get file dialog window in C#.net




Responses

Posted by: Raj.Trivedi on: 4/26/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hello

You can use File upload control that will allow you to select files.

Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 4/26/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
<form id="form1" runat="server" enctype="multipart/form-data">
<input type="file" id="myFile" name="myFile" />
<asp:Button runat="server" ID="btnUpload" OnClick="btnUploadClick" Text="Upload" />
</form>

protected void btnUploadClick(object sender, EventArgs e)
{
HttpPostedFile file = Request.Files["myFile"];

//check file was submitted
if (file != null && file.ContentLength > 0)
{
string fname = Path.GetFileName(file.FileName);
file.SaveAs(Server.MapPath(Path.Combine("~/App_Data/", fname)));
}
}

If this post helps you mark it as answer
Thanks

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sriramnandha on: 5/12/2013 [Member] Starter | Points: 25

Up
0
Down
.CS
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace openfiledialog
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
public string DefaultFileName = "H:/Dotnet/Asp.Net/openfiledialog/openfiledialog/";

protected void UploadButton_Click(object sender, EventArgs e)
{

if (FileUploader.HasFile)
try
{
FileUploader.SaveAs(Server.MapPath(DefaultFileName) +
FileUploader.FileName);
Label1.Text = "File name: " +
FileUploader.PostedFile.FileName + "<br>" +
FileUploader.PostedFile.ContentLength + " kb<br>" +
"Content type: " +
FileUploader.PostedFile.ContentType + "<br><b>Uploaded Successfully";

}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}
else
{
Label1.Text = "You have not specified a file.";
}

}
}
}

.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="openfiledialog.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>

</head>
<body>
<form id="form1" runat="server">
<div>
Select File &nbsp;<asp:FileUpload ID="FileUploader" runat="server" /><br />
<br />
<asp:Button ID="UploadButton" runat="server" Text="Upload" OnClick="UploadButton_Click" /><br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label><br />

</div>
</form>
</body>
</html>


hope this will help....


sriram

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Durgeshteli on: 5/16/2013 [Member] Starter | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="uploadfile.aspx.cs" Inherits="upload" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>

</head>
<body>
<form id="form1" runat="server">
<div>
Select File &nbsp;<asp:FileUpload ID="FileUploader" runat="server" /><br />
<br />
<asp:Button ID="UploadButton" runat="server" Text="Upload" OnClick="UploadButton_Click" /><br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label><br />

</div>
</form>
</body>
</html>

----------------code behind.............
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


}

public string DefaultFileName = "Upload/"; ---- is the folder where files are uploaded to


protected void UploadButton_Click(object sender, EventArgs e)
{

if (FileUploader.HasFile)
try
{
FileUploader.SaveAs(Server.MapPath(DefaultFileName) +
FileUploader.FileName );
Label1.Text = "File name: " +
FileUploader.PostedFile.FileName + "<br>" +
FileUploader.PostedFile.ContentLength + " kb<br>" +
"Content type: " +
FileUploader.PostedFile.ContentType + "<br><b>Uploaded Successfully";

}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}
else
{
Label1.Text = "You have not specified a file.";
}

}

}

Thanks

Dot Net topper

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Raj.Trivedi on: 5/16/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hello Check this

Database Tables and Stored Procs
-- Table for storing images

CREATE TABLE [dbo].[ImageGallery](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ImageName] [varchar](50) NULL,
[Images] [varchar](max) NULL
) ON [PRIMARY]

GO


-- Stored Procedure for Inserting Images
create proc [dbo].[InsertImages]
(
@ImageName varchar(50),
@Images varchar(max)
)
as
begin
insert into ImageGallery(ImageName,Images)
values
(@ImageName,@Images)
end

-- Stored Proc for Getting images from database
create proc [dbo].[GetImages]
as
begin
select * from ImageGallery
end
// HTML Mark up and Code behind Code for AddImage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddImage.aspx.cs" Inherits="AddImage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 134px;
}
.style3
{
width: 217px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<table class="style1">
<tr>
<td class="style2">
Browse Image</td>
<td class="style3">
<asp:FileUpload ID="fupdf" runat="server" />
</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td class="style2">
Image Name</td>
<td class="style3">
<asp:TextBox ID="xtxtImageName" runat="server"></asp:TextBox>
</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td class="style2">
&nbsp;</td>
<td class="style3">
<asp:Button ID="btnUpload" runat="server" Text="Upload and Save"
onclick="btnUpload_Click" />
</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td class="style2">
&nbsp;</td>
<td class="style3">
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td class="style2">
&nbsp;</td>
<td class="style3">
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
</table>

</div>

</form>
</body>
</html>


---- Code Behind.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Configuration;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.WebControls;

public partial class AddImage : System.Web.UI.Page
{

string errdesc = "0";

protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
// Connection String
string strconn = ConfigurationManager.ConnectionStrings["cnnLocal"].ConnectionString;
SqlConnection con = new SqlConnection(strconn);

// Getting the extension for the images

string extension = Path.GetExtension(fupdf.FileName);

// Getting the File name of Images

string filename = Path.GetFileName(fupdf.FileName);

// This will get the actual path for upload.

string uploadFolder = Request.PhysicalApplicationPath + "Images\\";

// This path will go in database because asp.net requires virtual path.

string upload = "~/Images/" + filename;

// Saving the images in the folder

fupdf.SaveAs(uploadFolder + filename);

string getfile = upload;

SqlCommand cmdins = new SqlCommand("InsertImages", con);
cmdins.CommandType = CommandType.StoredProcedure;
cmdins.Parameters.AddWithValue("@ImageName", SqlDbType.VarChar).Value = xtxtImageName.Text;
cmdins.Parameters.AddWithValue("@Images", getfile);
con.Open();
cmdins.ExecuteNonQuery();
Literal1.Text = "Data Saved";
con.Close();
}
catch (Exception ex)
{
errdesc = ex.Message;
Literal1.Text = errdesc;
}

}
}

Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response