469,621 Members | 1,796 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

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 1683
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by ok | last post: by
10 posts views Thread by John Smith | last post: by
5 posts views Thread by Grant Harmeyer | last post: by
3 posts views Thread by Kumarasamy.Mani | last post: by
3 posts views Thread by Gavin | last post: by
1 post views Thread by wenqiang7 | last post: by
4 posts views Thread by | last post: by
pezholio
1 post views Thread by pezholio | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.