Uploading an Image from a web page

  • Thread starter Thread starter Paul Gorman
  • Start date Start date
P

Paul Gorman

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 ><>
 
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"));
 
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.
 
Back
Top