input type=file does not always work

M

malhenry

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);

}

}
 
L

Laurent Bugnion [MVP]

Hi,
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.

<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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top