473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Problem with Image Uploading

39
Hi

Using FileUpload control to upload the image I have given the coding as
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.IO;
  11. using System.Data.SqlClient;
  12. using System.Net.Mail;
  13.  
  14.  
  15. public partial class _Default : System.Web.UI.Page
  16. {
  17.     protected void Page_Load(object sender, EventArgs e)
  18.     {
  19.  
  20.     }
  21.  
  22.     protected void Button1_Click1(object sender, EventArgs e)
  23.     {
  24.         string s = FileUpload1.FileName;
  25.         FileInfo ff = new FileInfo(s);
  26.         long num = ff.Length;
  27.         FileStream ft = new FileStream(s, FileMode.Open, FileAccess.Read);
  28.         BinaryReader br = new BinaryReader(ft);
  29.  
  30.         Byte[] data = br.ReadBytes((int)num);
  31.  
  32.         SqlConnection con = new SqlConnection("user id=sa;password=cast;database=Hello_Dr;server=AURORA-SERVER");
  33.         con.Open();
  34.         SqlCommand cmd = new SqlCommand("insert into imgdatastore values('" + data + "')", con);
  35.         cmd.ExecuteNonQuery();
  36.         Response.Redirect("Inserted");
  37.         con.Close();
  38.  
  39.     }
  40. }
  41.  
but while running, if I upload the jpg file named 'title_1.JPG'.

the error is

Could not find file 'title_1.JPG'.
May 30 '08 #1
2 1156
Hello,

FileUpload.FileName property - Gets the name of a file on a client to upload using the FileUpload control.

So in your case, You cannot use this property for initialize new instance of FileInfo class.
Try this code, I think it would be work properly:
Expand|Select|Wrap|Line Numbers
  1. protected void Button1_Click1(object sender, EventArgs e)
  2. {
  3.  
  4. if(FileUpload1.HasFile)
  5. {
  6. // get file name without path.
  7. string s = Path.GetFileName(FileUpload1.FileName);
  8.  
  9. int fileLen = FileUpload1.PostedFile.ContentLength;
  10. byte[] data = new byte[fileLen];
  11. data = FileUpload1.FileBytes;
  12.  
  13. SqlConnection con = new SqlConnection("user id=sa;password=cast;database=Hello_Dr;server=AUROR A-SERVER");
  14. con.Open();
  15. SqlCommand cmd = new SqlCommand("insert into imgdatastore values('" + data + "')", con);
  16. cmd.ExecuteNonQuery();
  17. Response.Redirect("Inserted");
  18. con.Close();
  19. }
  20. else
  21. {
  22.  // any message for user
  23. }
  24.  
  25. }
  26.  
Hope this helps.
May 30 '08 #2
suganya
39
Hello,

FileUpload.FileName property - Gets the name of a file on a client to upload using the FileUpload control.

So in your case, You cannot use this property for initialize new instance of FileInfo class.
Try this code, I think it would be work properly:
Expand|Select|Wrap|Line Numbers
  1. protected void Button1_Click1(object sender, EventArgs e)
  2. {
  3.  
  4. if(FileUpload1.HasFile)
  5. {
  6. // get file name without path.
  7. string s = Path.GetFileName(FileUpload1.FileName);
  8.  
  9. int fileLen = FileUpload1.PostedFile.ContentLength;
  10. byte[] data = new byte[fileLen];
  11. data = FileUpload1.FileBytes;
  12.  
  13. SqlConnection con = new SqlConnection("user id=sa;password=cast;database=Hello_Dr;server=AUROR A-SERVER");
  14. con.Open();
  15. SqlCommand cmd = new SqlCommand("insert into imgdatastore values('" + data + "')", con);
  16. cmd.ExecuteNonQuery();
  17. Response.Redirect("Inserted");
  18. con.Close();
  19. }
  20. else
  21. {
  22.  // any message for user
  23. }
  24.  
  25. }
  26.  
Hope this helps.

I modified the coding as above

But the Error is


The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
May 30 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: ok | last post by:
Hello, Q: How do I get image width and height before uploading an image? This because, I want to restrict people uploading huge files. Thanks in advance
2
by: Faree | last post by:
Hi, I am a PHP Developer.can any one help me on image upload and display of that image.what i want is uploading an image to a directory ,displaying of that image as a thumbnail.when i...
3
by: Pitcairnia | last post by:
The basic purpose of the site is for authenticated users to post event listings, which often include photographs. The user is faced with a page where they can insert all of the information about...
4
by: Himanshu | last post by:
hi, Can anybody tell me that thru asp.net using c#, how can we upload and download physical files in any table of SQL Server Database. the uploading part is running successfully but the...
2
by: prakharv | last post by:
Hi All, Below is the code which I am using to upload a jpeg file to the server. But the problem I am facing is that it is not copying the entire contents of the image file to the webserver and it...
8
by: ctiggerf | last post by:
I was hopeing someone could help me out here. Been stumped on this one all day. This function 1. Checks uploaded files. 2. Creates two resized images from each (a full size, and a...
7
by: dasrasmikant | last post by:
Dear Experts, I am falling in a trouble and need your help for solving it I want to get the width and heignt of an image before uploading it. Below is my code <script language="javascript">...
2
kamill
by: kamill | last post by:
Dear All, I am uploading a image file and after resize the image i am saving it. My script is working fine with small size images but when i am trying to upload big size image, i am getting below...
1
pezholio
by: pezholio | last post by:
Hi, It seems that every time I put together a new script to upload a file I always have problems, here's the latest one: I've got a form with two file input fields, when I submit the form,...
8
by: brahmaforces | last post by:
Hi Folks, I am using cherrypy and python. I am trying to get a user profile image to resize on the client side before uploading to the server. PHP has a gd library that does it it seems. Has...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.