473,387 Members | 1,512 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,387 software developers and data experts.

Uploading an Image from a web page

I am using the control type = file to perform a file
upload. When I click on the browse button to go select
the image I want to upload it places in the text box a
local path (C:\images\image.jpg for example). This is
where the image resides that I want to upload. Then I
proceed to click on upload so that I can run through my
code to do the upload process:

string strConnection = "some connection string";
SqlConnection oCon = new SqlConnection(strConnection);
SqlCommand oCom = new SqlCommand();

oCom.CommandText = "usp_msa_image_save_binary";
oCom.CommandType = CommandType.StoredProcedure;
oCom.Connection = oCon;

oCon.Open();

FileStream fs;
string strFileName = this.loFile.PostedFile.FileName;
fs = File.OpenRead(strFileName);

int intCount = (int)fs.Length;
int intSize;

byte[] buffer = new byte[intCount];
fs.Read(buffer,0,intCount);

oCom.Parameters.Add("@Image", SqlDbType.Image).Value =
buffer;

oCom.Parameters.Add("@itemID", SqlDbType.VarChar,
30).Value = "CEH03BA401"; //Request.QueryString.Get
("name");

if ( this.chkSmallImage.Checked )
{
oCom.Parameters.Add("@size", SqlDbType.Int).Value = 1;
intSize = 1;
}
else
{
oCom.Parameters.Add("@size", SqlDbType.Int).Value = 2;
intSize = 2;
}

oCom.ExecuteNonQuery();
string strItemNumber = Request.QueryString.Get("name");

string strUrl = "image_binary_get.aspx?itemnumber=" +
strItemNumber + "&size=" + intSize;

this.imgView.ImageUrl = strUrl;
It continues to fail at
fs = File.OpenRead(strFileName);
with the error message:
"Could not find part of the path "C:\images\image.jpg".

The one thing that I can be sure of is this. When i
attempt this process on the IIS Server where all this
resides then it works great. As soon as I try to upload
a file from some other computer on the network i get that
error. Any help would be greatly appreciated. Thanks in
advance.

Paul Gorman ><>
Nov 17 '05 #1
2 1857
this.loFile.PostedFile.FileName just returns the client side name of the
uploaded file.
Because your client & server is the same machine, your code is running
succesfully.

You should firstly save the uploaded file and then insert it into database.
As below:

this.loFile.PostedFile.SaveAs(Server.MapPath("/images/uploads/image.gif"));
....
....
fs = File.OpenRead(Server.MapPath("/images/uploads/image.gif"));

"Paul Gorman" <pg*****@satx.rr.com> wrote in message
news:0b****************************@phx.gbl...
I am using the control type = file to perform a file
upload. When I click on the browse button to go select
the image I want to upload it places in the text box a
local path (C:\images\image.jpg for example). This is
where the image resides that I want to upload. Then I
proceed to click on upload so that I can run through my
code to do the upload process:

string strConnection = "some connection string";
SqlConnection oCon = new SqlConnection(strConnection);
SqlCommand oCom = new SqlCommand();

oCom.CommandText = "usp_msa_image_save_binary";
oCom.CommandType = CommandType.StoredProcedure;
oCom.Connection = oCon;

oCon.Open();

FileStream fs;
string strFileName = this.loFile.PostedFile.FileName;
fs = File.OpenRead(strFileName);

int intCount = (int)fs.Length;
int intSize;

byte[] buffer = new byte[intCount];
fs.Read(buffer,0,intCount);

oCom.Parameters.Add("@Image", SqlDbType.Image).Value =
buffer;

oCom.Parameters.Add("@itemID", SqlDbType.VarChar,
30).Value = "CEH03BA401"; //Request.QueryString.Get
("name");

if ( this.chkSmallImage.Checked )
{
oCom.Parameters.Add("@size", SqlDbType.Int).Value = 1;
intSize = 1;
}
else
{
oCom.Parameters.Add("@size", SqlDbType.Int).Value = 2;
intSize = 2;
}

oCom.ExecuteNonQuery();
string strItemNumber = Request.QueryString.Get("name");

string strUrl = "image_binary_get.aspx?itemnumber=" +
strItemNumber + "&size=" + intSize;

this.imgView.ImageUrl = strUrl;
It continues to fail at
fs = File.OpenRead(strFileName);
with the error message:
"Could not find part of the path "C:\images\image.jpg".

The one thing that I can be sure of is this. When i
attempt this process on the IIS Server where all this
resides then it works great. As soon as I try to upload
a file from some other computer on the network i get that
error. Any help would be greatly appreciated. Thanks in
advance.

Paul Gorman ><>

Nov 17 '05 #2
When you want to upload the image from a network drive, then path
"C:\Foo\Blah blah" is not local to the web server. So it will try to look
for it and then give you the error message that you are getting. You will
have to follow the netrwork drive access procudre to do it. Like
"\\MyRemoteServer\C$\Foo\Blah" assuming that you have a share created on
your network drive and your web server has permissions to access the files
there. This could be a big security problem for you.

--
Naveen K Kohli
http://www.netomatix.com
"Paul Gorman" <pg*****@satx.rr.com> wrote in message
news:0b****************************@phx.gbl...
I am using the control type = file to perform a file
upload. When I click on the browse button to go select
the image I want to upload it places in the text box a
local path (C:\images\image.jpg for example). This is
where the image resides that I want to upload. Then I
proceed to click on upload so that I can run through my
code to do the upload process:

string strConnection = "some connection string";
SqlConnection oCon = new SqlConnection(strConnection);
SqlCommand oCom = new SqlCommand();

oCom.CommandText = "usp_msa_image_save_binary";
oCom.CommandType = CommandType.StoredProcedure;
oCom.Connection = oCon;

oCon.Open();

FileStream fs;
string strFileName = this.loFile.PostedFile.FileName;
fs = File.OpenRead(strFileName);

int intCount = (int)fs.Length;
int intSize;

byte[] buffer = new byte[intCount];
fs.Read(buffer,0,intCount);

oCom.Parameters.Add("@Image", SqlDbType.Image).Value =
buffer;

oCom.Parameters.Add("@itemID", SqlDbType.VarChar,
30).Value = "CEH03BA401"; //Request.QueryString.Get
("name");

if ( this.chkSmallImage.Checked )
{
oCom.Parameters.Add("@size", SqlDbType.Int).Value = 1;
intSize = 1;
}
else
{
oCom.Parameters.Add("@size", SqlDbType.Int).Value = 2;
intSize = 2;
}

oCom.ExecuteNonQuery();
string strItemNumber = Request.QueryString.Get("name");

string strUrl = "image_binary_get.aspx?itemnumber=" +
strItemNumber + "&size=" + intSize;

this.imgView.ImageUrl = strUrl;
It continues to fail at
fs = File.OpenRead(strFileName);
with the error message:
"Could not find part of the path "C:\images\image.jpg".

The one thing that I can be sure of is this. When i
attempt this process on the IIS Server where all this
resides then it works great. As soon as I try to upload
a file from some other computer on the network i get that
error. Any help would be greatly appreciated. Thanks in
advance.

Paul Gorman ><>

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

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
10
by: John Smith | last post by:
I know that uploading an image to a database has been covered, oh, about 3 trillion times. However, I haven't found anything covering uploading to a MySQL database with .net. Please don't...
5
by: Grant Harmeyer | last post by:
I have an application that uses FreeTextBox 2.0 (http://www.freetextbox.com). FreeTextBox is a rich text editor that behaves similarly to MS Word. The FreeTextBox control has a button to insert...
3
by: Kumarasamy.Mani | last post by:
Hi all, I'm having the following issue in image preview. I'm having an File Upload option in my web page, there i'm having one more option preview. When the user is uploading the image using...
3
by: Gavin | last post by:
I need some help, I need the code to allow people that visit my website to be able to upload pictures to a file on my web server. I have been able to get close, but not quite there yet. Set fso...
1
by: wenqiang7 | last post by:
I am encountering a very strang problem with file uploading in my ASP.Net page. When we try to upload certain file, we'll get an error msg of "Cannot find server or DNS Error". We are running...
4
by: | last post by:
Can someone offer or point me to some sample code or advice on how to upload a document from a web page and store it into a SQL Server image field? And then reverse the process and serve up the file...
1
by: Kunal Nandi | last post by:
can any one give me the code for uploading and retriving image using Blob, with jsp at front end and oracle8i at the back end ??????? i have tried this using long raw datatype i was able to upload...
1
by: thulaseeram | last post by:
I am using iframe to store uploaded images, it is uploading fine in IE but it is not happening in firefox means first time it is uploading image if i try to upload second image it is not calling even...
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.