HighTechTalks DotNet Forums  

input type=file does not always work

ASP.net Web Services microsoft.public.dotnet.framework.aspnet.webservices


Discuss input type=file does not always work in the ASP.net Web Services forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
malhenry
 
Posts: n/a

Default input type=file does not always work - 03-01-2007 , 03:28 PM






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

}

}



Reply With Quote
  #2  
Old   
Laurent Bugnion [MVP]
 
Posts: n/a

Default Re: input type=file does not always work - 03-01-2007 , 05:32 PM






Hi,

malhenry wrote:
Quote:
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>

Quote:
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


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.