input type=file does not always work

Discussion in 'ASP .Net Web Services' started by malhenry, Mar 1, 2007.

  1. malhenry

    malhenry Guest

    Hi,

    I am using asp.net 2.0. The code below has a special case where it does not
    work.

    If I browse to a file or enter a fullpath (i.e. c:\x.x), then the
    OnServerclick method will be called as expected. This is true regardless of
    whether or not C:\x.x exists.

    However, if I just enter x.x (without a drive specification), then the
    Onserverclick method does NOT get called. Do you know why?

    Thanks!

    <%@ Page Language="C#" MasterPageFile="~/xxx/MasterPage.master"
    AutoEventWireup="true" CodeFile="myfile.aspx.cs" Inherits="yyy" Title="zzz" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="maincontent" Runat="Server">

    <div id="myedit">

    <hr>

    <asp:label id="Message" Text="Select a file for the background to upload to
    the database" runat="server"/>

    <hr>

    <!--

    <b>File Name for Download:</b><br>

    <input id="MyFileName" type="text" runat="server">

    <P>

    -->

    <b>File:</b><br>

    <input id="myImage"

    type="file"

    runat="server">

    <br><br>

    <input id="Submit1" type=submit

    value="Upload!"

    OnServerclick="UploadBtn_Click"

    runat="server">

    <br><br>

    <!--

    <br><br>

    <input id="Submit2" type=submit

    value="Download!"

    OnServerclick="DownloadBtn_Click"

    runat="server">

    <br><br><br>

    -->

    </div>

    </asp:Content>

    using System;

    using System.IO;

    using System.Data;

    using System.Configuration;

    using System.Collections;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    using System.Data.SqlClient;



    public partial class yyy : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e)

    {



    }



    public void UploadBtn_Click (Object sender, EventArgs e)

    {

    // Uploads a binary file into an image field in a sql db


    string myUserName = Page.User.Identity.Name;



    //Get the posted file

    Stream fileDataStream = BreakingImage.PostedFile.InputStream;



    //Get length of file

    int fileLength = BreakingImage.PostedFile.ContentLength + 1;//lrm added 1



    if (fileLength == 1)

    {

    Message.Text = "Your file is empty or does not exist!";

    return;

    }



    //Create a byte array with file length

    byte[] fileData = new byte[fileLength];



    //Read the stream into the byte array

    fileDataStream.Read(fileData,0,fileLength);



    System.Guid userid = GetUserid();



    //now create the connection, the sql statement, and the sql parameters



    string connectionString =
    ConfigurationManager.ConnectionStrings["qqq"].ConnectionString;

    SqlConnection connection = new SqlConnection(connectionString);

    SqlCommand command = new SqlCommand("INSERT INTO mytable
    (CreatedOn,BImage,CreatedBy)" +

    "VALUES (@CreatedOn,@BImage,@CreatedBy)", connection);



    //AddParamToSQLCmd(sqlCmd, "@CreatedOn", SqlDbType.DateTime, 0,
    ParameterDirection.Input, System.DateTime.Now.ToUniversalTime());

    SqlParameter paramTitle = new SqlParameter("@CreatedOn",
    SqlDbType.DateTime, 0);

    paramTitle.Value = System.DateTime.Now.ToUniversalTime();

    command.Parameters.Add(paramTitle);



    SqlParameter paramData = new SqlParameter ("@BImage", SqlDbType.Image);

    paramData.Value = fileData;

    command.Parameters.Add(paramData);



    SqlParameter paramType = new SqlParameter ("@CreatedBy",
    SqlDbType.UniqueIdentifier,0);

    paramType.Value = userid;

    command.Parameters.Add(paramType);



    //open the connection and execute the query



    connection.Open();

    command.ExecuteNonQuery();

    connection.Close();



    //now do some cleanup



    Message.Text="Your file has uploaded";

    //MyFileName.Value = "";

    }




    //----------------------------------------------------------------------------------------



    public System.Guid GetUserid()

    {

    System.Guid myUserid;

    string myUseridStr;



    string myUserName = Page.User.Identity.Name;



    string myQuery = "SELECT UserId FROM ASPNET_USERS " +

    "WHERE Username='" +

    myUserName + "'";



    //now create the connection, the sql statement, and the sql parameters



    string connectionString =
    ConfigurationManager.ConnectionStrings["qqq"].ConnectionString;

    SqlConnection connection = new SqlConnection(connectionString);



    //open the connection and execute the query



    connection.Open();



    // Create a SQL command



    SqlCommand DBCmd = new SqlCommand(myQuery, connection);



    SqlDataReader myDataReader;

    myDataReader = DBCmd.ExecuteReader();



    myDataReader.Read();

    myUseridStr = myDataReader[0].ToString();

    myUserid = (System.Guid)myDataReader[0];

    myDataReader.Close();



    connection.Close();



    return (myUserid);

    }

    }
     
    malhenry, Mar 1, 2007
    #1
    1. Advertising

  2. Hi,

    malhenry wrote:
    > Hi,
    >
    > I am using asp.net 2.0. The code below has a special case where it does not
    > work.
    >
    > If I browse to a file or enter a fullpath (i.e. c:\x.x), then the
    > OnServerclick method will be called as expected. This is true regardless of
    > whether or not C:\x.x exists.


    <snip>

    > <input id="Submit1" type=submit
    >
    > value="Upload!"
    >
    > OnServerclick="UploadBtn_Click"
    >
    > runat="server">


    <snip>

    Just wondering, why not use a proper asp:FileUpload control?

    Greetings,
    Laurent
    --
    Laurent Bugnion [MVP ASP.NET]
    Software engineering, Blog: http://www.galasoft-LB.ch
    PhotoAlbum: http://www.galasoft-LB.ch/pictures
    Support children in Calcutta: http://www.calcutta-espoir.ch
     
    Laurent Bugnion [MVP], Mar 1, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Roland_M=FCller?=
    Replies:
    1
    Views:
    1,628
    =?ISO-8859-1?Q?Roland_M=FCller?=
    Nov 21, 2005
  2. Doug Fort
    Replies:
    5
    Views:
    284
    Doug Fort
    Aug 6, 2004
  3. Roberto
    Replies:
    3
    Views:
    310
  4. Wayne
    Replies:
    1
    Views:
    74
  5. Tarkeshwar
    Replies:
    2
    Views:
    192
Loading...

Share This Page