i have problem with the lable when i upload file into database it not show any message
c#
protected void btnUpload_Click(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string constr = ConfigurationManager.ConnectionStrings["homeworkConnectionString2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "insert into tblFiles (FileName,ContentType,Number,Date,Data) values (#Name, #ContentType,#number,getDate(), #Data)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("#Name", filename);
cmd.Parameters.AddWithValue("#ContentType", contentType);
cmd.Parameters.AddWithValue("#number", Session["id"].ToString());
cmd.Parameters.AddWithValue("#Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
fname.Visible = true;
fname.Text = "file Has been uploaded";
}
}
}
asp.net code
<asp:Label ID="fname" runat="server" Text="Label" Visible="False"></asp:Label>
the lable work with insert value but not work with the uploading file
protected void btnUpload_Click(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string constr = ConfigurationManager.ConnectionStrings["homeworkConnectionString2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "insert into tblFiles (FileName,ContentType,Number,Date,Data) values (#Name, #ContentType,#number,getDate(), #Data)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("#Name", filename);
cmd.Parameters.AddWithValue("#ContentType", contentType);
cmd.Parameters.AddWithValue("#number", Session["id"].ToString());
cmd.Parameters.AddWithValue("#Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
fname.Visible = true;
fname.Text = "file Has been uploaded";
}
Try Like this...
Related
I am making a file uploader using html tags. I have id, Name and Data in my table. Data is storing the contents of the file in binary format in a column of datatype varbinary(max). I am able to successfully upload the file and I am also able to read the byte array back.
Code for upload at code behind:
protected void Upload(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "insert into FileUploader2 values (#Name, #ContentType, #Data)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("#Name", filename);
cmd.Parameters.AddWithValue("#ContentType", contentType);
cmd.Parameters.AddWithValue("#Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Context.Response.Write("Uploaded Successfully!");
}
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
I am also creating a grid which shows me list of files along with an icon which when clicked will view me the file after reading the byte array. I have a JS function on that onclick which I am accessing through WebMethod at code-behind. Following is my code when user clicks on view icon:
[System.Web.Services.WebMethod]
public static void ShowDocument()
{
string filename = string.Empty;
string contentType = string.Empty;
byte[] bytes = null;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
con.Open();
using (SqlCommand com = new SqlCommand("SELECT * FROM FileUploader2", con))
{
using (SqlDataReader reader = com.ExecuteReader())
{
if (reader.Read())
{
filename = (string)reader["Name"];
contentType = (string)reader["ContentType"];
bytes = (byte[])reader["Data"];
}
}
}
}
System.Web.HttpContext.Current.Response.ContentType = contentType;
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;
filename=" + filename);
System.Web.HttpContext.Current.Response.OutputStream.Write(bytes, 0, bytes.Length);
System.Web.HttpContext.Current.Response.Flush();
}
This code is working fine as well and correctly reads all the columns. Now after reading, I want to view the file as well in a pop window. How can I do so? I think I have to take contenttype to let it know what type of file I want to open and then use the bytes to do so. But I have no idea how to achieve that.
I need to save a file size in database table while uploading a file in using upload control.
i am simple pitching my upload control code here.
Please take a look.
protected void UploadFileControl(object sender, EventArgs e)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
using (SqlConnection con = new SqlConnection(con))
{
string str = "INSERT INTO XYZ VALUES (#FileName, #ContentType, #Data)";
using (SqlCommand cmd = new SqlCommand(str))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("#FileName", fileName);
cmd.Parameters.AddWithValue("#ContentType", contentType);
cmd.Parameters.AddWithValue("#Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
}
This is simple upload control code.
Hello Im using this C# code for images upload.
protected void UploadFile(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
string query = "INSERT INTO foto(FileName, ContentType, Content) VALUES (#FileName, #ContentType, #Content)";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("#FileName", filename);
cmd.Parameters.AddWithValue("#ContentType", contentType);
cmd.Parameters.AddWithValue("#Content", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
I upload the image in a longblob field, then to show the image I am using C# WebService, AJAX, JavaScript, converting the image to Base64String but image is displayed like if do not exist.
Here is my Base64String:
Base64String
As you can see the problem is with this extra characters:
AAEAAAD/////AQAAAAAAAAAPAQAAAHgBAAAC
Why this happen? And how can I solve it?
can you replace the bellowed lines with your above code
protected void UploadFile(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
System.Drawing.Image imagetuUpload = System.Drawing.Image.FromStream(fs);
Bitmap bitmap = new Bitmap(imagetuUpload);
System.IO.MemoryStream stream = new MemoryStream();
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Position = 0;
byte[] upproimag = new byte[stream.Length + 1];
stream.Read(upproimag, 0, upproimag.Length);
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
string query = "INSERT INTO foto(FileName, ContentType, Content) VALUES (#FileName, #ContentType, #Content)";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("#FileName", filename);
cmd.Parameters.AddWithValue("#ContentType", contentType);
cmd.Parameters.AddWithValue("#Content", upproimag);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
i hop it may help you :)
long time back i was in same issue i do not remember other than doing this at that time:
I check if I am wrong or not going here:
http://jsfiddle.net/hpP45/
I try to decode first(google bse64 decoder )
https://www.base64decode.org/
and save decoded binary data as an jpeg file and open it.
if it does not open then i guess,there might be issue on your base64 encoding
I am new to programming. I want to be able to upload files on a website that save into my database when I upload the file and the send an email of the files that I uploaded to two other parties
how can I achieve this goal?
This is the code that I currently have but I get errors of file name does not exist in current context?
protected void Upload(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "insert into tblFiles values (#Name, #ContentType, #Data)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("#File Name", filename);
cmd.Parameters.AddWithValue("#ContentType", contentType);
cmd.Parameters.AddWithValue("#Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void Upload_Bt1_Click(object sender, EventArgs e)
{
MailMessage mailObj = new MailMessage(
txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
SmtpClient SMTPServer = new SmtpClient("127.0.0.1");
try
{
SMTPServer.Send(mailObj);
}
catch (Exception ex)
{
Label1.Text = ex.ToString();
}
}
I'm developing an admin panel and a web service but how to add an image data into SQL Server database and retrieve it in admin panel in GridView? Everything adds fine, shows fine except the images. I'm using HTML Input (file) control. No errors just image is not displaying but lblTest shows accordingly.
And how to make my Button can ADD and Refresh so user can see the new data instantly.
Thank you!
Here is my C# code:
protected void Page_Load(object sender, EventArgs e)
{
showSplash();
}
string connStr = WebConfigurationManager.ConnectionStrings["connection"].ConnectionString;
Int32 fileLength = 0;
protected void ButtonTest_Click(object sender, EventArgs e)
{
HttpPostedFile uploadFile = FileLogo.PostedFile;
fileLength = uploadFile.ContentLength;
if (fileLength == 0)
{
string filePath = Server.MapPath(#"\icon\no-photo-icon.jpg");
string fileName = Path.GetFileName(filePath);
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
fileLength = (Int32)fs.Length;
Byte[] fileByteArr = new Byte[fileLength];
fs.Read(fileByteArr, 0, fileLength);
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand("insert into SPLASH (VersionNumber, SplashLabel,LoginID) values (#VersionNumber,#SplashLabel,#LoginID)", conn);
cmd.Parameters.AddWithValue("#VersionNumber", txtVnum.Value);
cmd.Parameters.AddWithValue("#SplashLabel", txtSpLabel.Value);
cmd.Parameters.AddWithValue("#LoginID", txtYourID.Value);
cmd.Parameters.AddWithValue("#ImageData", fileByteArr);
cmd.Parameters.AddWithValue("#ImageContentType", "image/jpg");
cmd.Parameters.AddWithValue("#ImageSize", fileLength);
lbltest.Text = "added fine with no file";
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
conn.Dispose();
cmd.Dispose();
}
else
{
Byte[] fileByteArray = new Byte[fileLength];
Stream streamObject = uploadFile.InputStream;
streamObject.Read(fileByteArray, 0, fileLength);
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand("insert into SPLASH (VersionNumber, SplashLabel,LoginID) values (#VersionNumber,#SplashLabel,#LoginID)", conn);
cmd.Parameters.AddWithValue("#VersionNumber", txtVnum.Value);
cmd.Parameters.AddWithValue("#SplashLabel", txtSpLabel.Value);
cmd.Parameters.AddWithValue("#LoginID", txtYourID.Value);
cmd.Parameters.AddWithValue("#ImageData", fileByteArray);
cmd.Parameters.AddWithValue("#ImageContentType", "image/jpg");
cmd.Parameters.AddWithValue("#ImageSize", fileLength);
lbltest.Text = "added fine";
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
conn.Dispose();
cmd.Dispose();
}
}
private void showSplash()
{
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand("select * from SPLASH", conn);
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
System.Data.DataTable dt = new System.Data.DataTable();
dt.Load(rdr);
GridViewAddSplash.DataSource = dt;
GridViewAddSplash.DataBind();
conn.Close();
conn.Dispose();
cmd.Dispose();
}
My other related question links:
Ajax tabContainer: