Uploading an Image from a web page

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default 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 ><>


    Paul Gorman Guest

  2. Similar Questions and Discussions

    1. Don't want an /image folder when uploading images
      I want to be able to upload image files from my local computer using Contribute CS3 without having the program create an IMAGES folder. I don't...
    2. Uploading image
      I'm got a cool uploading images tutorial from Asfusion.com. I got it working but this is my issue. I created a db table called "photos", in the...
    3. Uploading Image to server problems
      I am trying to upload an image to the server from the browser using a form and an ADODB binary stream. The file successfully saves to the correct...
    4. uploading imageready image map
      hello, i'm pretty new to photoshop and have just made my first image map with rollovers using photoshop and imageready. when i preview it in the...
    5. getting image width/height before uploading
      Hello, Q: How do I get image width and height before uploading an image? This because, I want to restrict people uploading huge files. ...
  3. #2

    Default Re: Uploading an Image from a web page

    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" <pgorman@satx.rr.com> wrote in message
    news:0b4501c35ceb$77682610$a401280a@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 ><>
    >
    >

    Bülent Keskin Guest

  4. #3

    Default Re: Uploading an Image from a web page

    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
    [url]http://www.netomatix.com[/url]
    "Paul Gorman" <pgorman@satx.rr.com> wrote in message
    news:0b4501c35ceb$77682610$a401280a@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 ><>
    >
    >

    Naveen K Kohli Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139