473,666 Members | 2,302 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using HtmlInputFile problem

Hi,

I'm trying to upload a file and I do that with this code, but there is a
thing which doesn't work. I can't understand how to check if it isn't
written the path to the file(the text field of <input> control is empty) or
it is.
I have tryed with the following:
if( filMyFile.Poste dFile != null) //HtmlInputFile filMyFile;
{
code
}
else
{
code
}

and with:
if(filMyFile.Va lue=="")
{
code
}
else
{
code
}
but everytime( when the text filed with the path is empty or filled) the if
statement works in same way.
How could be checked if the path is filled or it isn't?
Here is my code.Thank you in advance!

private void Button1_Click(o bject sender, System.EventArg s e)
{
msg.Text="";
if(filMyFile.Po stedFile != null)
{
try
{
// Get a reference to PostedFile object
HttpPostedFile myFile = filMyFile.Poste dFile;
// Get size of uploaded file
int nFileLen = myFile.ContentL ength;
if(myFile.Conte ntType.CompareT o("image/gif")==0)
{
// Allocate a buffer for reading of the file
byte[] myData = new byte[nFileLen];
// Read uploaded file from the Stream
myFile.InputStr eam.Read(myData , 0, nFileLen);
string strFilename = Path.GetFileNam e(myFile.FileNa me);
string strDir = "c:\\Inetpub\\w wwroot\\Estates \\Files\\" +
Session["usrName"].ToString();
Directory.Creat eDirectory( strDir);
string
strRoute=MapPat h("/Estates/Files/"+Session["usrName"].ToString()+"/"+strFile
name);
//string strFilename = Path.GetFileNam e(myFile.FileNa me);
//string strRoute=MapPat h("/Estates/Files/"+strFilena me );
WriteToFile(str Route,myData);
msg.Text="The file was attached";
}
else
{
msg.Text="This file is not a gif!!!";
}
}
catch(Exception exc)
{
msg.Text = "Error by saving: " + exc.ToString();
}
}
else
{
msg.Text="Pleas e, atach your file!";
return;
}
}
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.
Nov 18 '05 #1
1 1670
I use this JavaScript (see below) code to check for the existence/selection of a file but instead of writing the client code directly into the aspx page I use the RegisterStartup Script method. In this script I validate the user input by allowing them to enter just ..doc files. It's more efficient since the page has no need to make a roundtrip just for validation purposes. Run this code in the Page_Load event handler.

In my case, the HTML input file is a HTML server control and its name (client attribute and case sensitive, do not confuse with ID server-side attribute) is txtFile and the ASP.NET button is named btnEnviar. Set the encType="multip art/form-data" attribute in the HTML form of your upload page

Hope this helps
Alan Ferrandiz
MCSD, MCT, MSF Practitioner

Dim sb As New System.Text.Str ingBuilder()
sb.Append("<scr ipt language=""Java Script"">")
sb.Append(vbNew Line)
sb.Append(" function btnEnviarOnClic k()")
sb.Append(vbNew Line)
sb.Append(" {")
sb.Append(vbNew Line)
sb.Append(" var archivo = document.Form1. txtFile.value;" )
sb.Append(vbNew Line)
sb.Append(" if (archivo.length )")
sb.Append(vbNew Line)
sb.Append(" {")
sb.Append(vbNew Line)
sb.Append(" var extension = document.Form1. txtFile.value;" )
sb.Append(vbNew Line)
sb.Append(" extension = extension.subst ring(extension. length-3,extension.len gth);")
sb.Append(vbNew Line)
sb.Append(" extension = extension.toLow erCase();")
sb.Append(vbNew Line)
sb.Append(" if(extension != 'doc')")
sb.Append(vbNew Line)
sb.Append(" {")
sb.Append(vbNew Line)
sb.Append(" window.alert("" Selecciono un archivo ."" + extension + "", por favor seleccione un archivo .doc"");")
sb.Append(vbNew Line)
sb.Append(" document.Form1. txtFile.focus() ;")
sb.Append(vbNew Line)
sb.Append(" return false;")
sb.Append(vbNew Line)
sb.Append(" }")
sb.Append(vbNew Line)
sb.Append(" else")
sb.Append(vbNew Line)
sb.Append(" return true;")
sb.Append(vbNew Line)
sb.Append(" }")
sb.Append(vbNew Line)
sb.Append(" else")
sb.Append(vbNew Line)
sb.Append(" {")
sb.Append(vbNew Line)
sb.Append(" window.alert("" Debe seleccionar un archivo"");")
sb.Append(vbNew Line)
sb.Append(" document.Form1. txtFile.focus() ;")
sb.Append(vbNew Line)
sb.Append(" return false;")
sb.Append(vbNew Line)
sb.Append(" }")
sb.Append(vbNew Line)
sb.Append(" }")
sb.Append(vbNew Line)
sb.Append("</script>")

Page.RegisterSt artupScript("", sb.ToString)
btnEnviar.Attri butes.Add( "onClick", "return btnEnviarOnClic k();")


"Viktor Popov" <vi****@yahoo.c om> escribió en el mensaje news:uJ******** ******@tk2msftn gp13.phx.gbl...
Hi,

I'm trying to upload a file and I do that with this code, but there is a
thing which doesn't work. I can't understand how to check if it isn't
written the path to the file(the text field of <input> control is empty) or
it is.
I have tryed with the following:
if( filMyFile.Poste dFile != null) //HtmlInputFile filMyFile;
{
code
}
else
{
code
}

and with:
if(filMyFile.Va lue=="")
{
code
}
else
{
code
}
but everytime( when the text filed with the path is empty or filled) the if
statement works in same way.
How could be checked if the path is filled or it isn't?
Here is my code.Thank you in advance!



private void Button1_Click(o bject sender, System.EventArg s e)
{
msg.Text="";
if(filMyFile.Po stedFile != null)
{
try
{
// Get a reference to PostedFile object
HttpPostedFile myFile = filMyFile.Poste dFile;
// Get size of uploaded file
int nFileLen = myFile.ContentL ength;
if(myFile.Conte ntType.CompareT o("image/gif")==0)
{
// Allocate a buffer for reading of the file
byte[] myData = new byte[nFileLen];
// Read uploaded file from the Stream
myFile.InputStr eam.Read(myData , 0, nFileLen);
string strFilename = Path.GetFileNam e(myFile.FileNa me);
string strDir = "c:\\Inetpub\\w wwroot\\Estates \\Files\\" +
Session["usrName"].ToString();
Directory.Creat eDirectory( strDir);
string
strRoute=MapPat h("/Estates/Files/"+Session["usrName"].ToString()+"/"+strFile
name);
//string strFilename = Path.GetFileNam e(myFile.FileNa me);
//string strRoute=MapPat h("/Estates/Files/"+strFilena me );
WriteToFile(str Route,myData);
msg.Text="The file was attached";
}
else
{
msg.Text="This file is not a gif!!!";
}
}
catch(Exception exc)
{
msg.Text = "Error by saving: " + exc.ToString();
}
}
else
{
msg.Text="Pleas e, atach your file!";
return;
}
}


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.729 / Virus Database: 484 - Release Date: 27.7.2004 a.

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2947
by: R Reyes | last post by:
I am coding a forum and am having problems passing around the attachment file (as an HtmlInputFile) from page to page. Sometimes the users may want to preview their posts with the attachment specs (filesize, content-type, etc..) before submitting. i have tried storing it in a session like so: HtmlInputFile uploadFile = new HtmlInputFile(); Session = uploadFile.PostedFile; Then retrieving it, though I can't seem to figure out if the...
1
2123
by: Newbie | last post by:
i have a c# asp.net webform that has an HtmlInputFile and a listbox (to hold the filenames to be uploaded). but HtmlInputFile.PostedFile and HtmlInputFile.Value are readonly properties. i have to populate the listbox prior to showing it to the user. the listbox will already contain items (old files) in it when the user first see it. i am trying to add files to the HtmlInputFile collection programmatically before letting the user add or...
0
865
by: Frank | last post by:
In my aspx page, I use HtmlInputFile to ask user to select a file. When I select the file and hit submit button, 60% of the time I get "The page cannot be displayed". A postback event didn't happen at all. After lots of testing I find out it's the problem with HtmlInputFile html control. If I select a file and wait a while before I hit submit button, I am fine. BUT if I click to fast, I will get an error page. Did anybody have this...
9
1677
by: Rob Meade | last post by:
Hi all, I've got some code here that builds a page dynamically, ie, added a table, rows, cells, controls in the cells and so on... Everything has gone fine until now, I need to add a 'File Input' box (ie, the HTML <input type="file"> etc), I've got this: Dim txtFile as HtmlInputFile
2
1668
by: Augusto Cesar via DotNetMonster.com | last post by:
Hi, I want to customize the HtmlInputFile. I wanna something like an image button to play the "browse" button hole and hide the textbox. Is that possible? I also have tried to hide an HtmlInputFile in my page and added some javascript code to a button to fire the click() event of the HtmlInputFile. It worked for a moment, but when the submit button is clicked the HtmlInputFile looses its value. This solution was described in the...
3
4731
by: UJ | last post by:
Guys, I know this isn't the appropriate place to post this because it's HTML not ASP.Net but I need some guidance. I have a web page with an HTMLInputFile on it. The person enters the stuff, that's all great. They go to the next page and I allow them to click a button to go back a page to change values. When I go back one page, I want the HTMLInputFile to be filled in with the previously loaded value (which I have saved somewhere) but...
7
1756
by: Buddy Ackerman | last post by:
I created this class Public Class HTMLFileInput : Inherits System.Web.UI.HtmlControls.HtmlInputFile Public Property Data As String Get Return ViewState("HTMLFileInput.Data") End Get Set (ByVal Value As String)
3
4555
by: Dave Adler | last post by:
Is there any way to retain the value of an HtmlInputFile control through a postback? I do some server side validation on the page when it is submitted and if an error occurs on the page the HtmlInputFile control value is blank after the postback. The EnableViewState for the control is set to true. I can't put the postedFile.filename value in a hidden control and then put it back into the HtmlInputFile control on postback because the...
1
1689
by: nes | last post by:
Hi all, For uploading files i am using the HtmlInputFile. Now I want to do 2 things with it. First I want to give the button a specific style and second when a postback happens I don't want the selected file to disappear from the HtmlInputFile. Now on the web I found the next thing: <html> <head>
8
1855
by: Nathan Sokalski | last post by:
I have a System.Web.UI.HtmlControls.HtmlInputFile control that I use to submit files. After the file is successfully submitted, I want the field to be reset so that the user knows the file was submitted. However, ASP.NET does not let you change the Value property of an HtmlInputFile control. How can I reset the HtmlInputFile control to it's original state? Thanks. -- Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
0
8443
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7385
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.