Code is working in normal webform aspx page but same code not working in Master page
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<title></title>
<style>
input[type="file"]
{
display: block;
}
.imageThumb
{
max-height: 75px;
border: 2px solid;
margin: 10px 10px 0 0;
padding: 1px;
}
</style>
<script src="http://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("[id*=files]").click(function () {
if (window.File && window.FileList && window.FileReader) {
$("#files").on("change", function (e) {
var files = e.target.files,
filesLength = files.length;
for (var i = 0; i < filesLength; i++) {
var f = files[i]
var fileReader = new FileReader();
fileReader.onload = (function (e) {
var file = e.target;
$("<img></img>", {
class: "imageThumb",
src: e.target.result,
title: file.name,
name: 'test'
}).insertAfter("#files");
});
fileReader.readAsDataURL(f);
}
});
} else { alert("Your browser doesn't support to File API") }
});
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<h2>
Cubicide Technologies Upload Multiple Images</h2>
<input type="file" id="files" name="files[]" multiple />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
<br />
<br />
</div>
</body>
</asp:Content>
private void SaveImageFile(string fileName, string contentType, Byte[] bytes)
{
string constring = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SqlConnection con = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand("insert into tblFiles (Name, ContentType, Data) values (@Name, @ContentType, @Data)", con);
cmd.CommandType = CommandType.Text;
con.Open();
cmd.Parameters.AddWithValue("@Name", SqlDbType.VarChar).Value = fileName;
cmd.Parameters.AddWithValue("@ContentType", SqlDbType.VarChar).Value = contentType;
cmd.Parameters.AddWithValue("@Data", SqlDbType.VarBinary).Value = bytes;
cmd.ExecuteNonQuery();
con.Close();
}
protected void btnUpload_Click(object sender, EventArgs e)
{
string contentType = "image/png";
string fileName = string.Empty;
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile postedFile = Request.Files[i];
if (postedFile.ContentLength > 0)
{
fileName = Path.GetFileName(postedFile.FileName);
Stream fs = postedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
// Call saving method by accessing the parameters
SaveImageFile(fileName, contentType, bytes);
}
}
Response.Write("Photo uploaded Successfully");
//this.BindGrid();
}
public class ImageFiles
{
public string Name { get; set; }
public byte[] Data { get; set; }
}