using HtmlInputFile problem

V

Viktor Popov

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.PostedFile != null) //HtmlInputFile filMyFile;
{
code
}
else
{
code
}

and with:
if(filMyFile.Value=="")
{
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(object sender, System.EventArgs e)
{
msg.Text="";
if(filMyFile.PostedFile != null)
{
try
{
// Get a reference to PostedFile object
HttpPostedFile myFile = filMyFile.PostedFile;
// Get size of uploaded file
int nFileLen = myFile.ContentLength;
if(myFile.ContentType.CompareTo("image/gif")==0)
{
// Allocate a buffer for reading of the file
byte[] myData = new byte[nFileLen];
// Read uploaded file from the Stream
myFile.InputStream.Read(myData, 0, nFileLen);
string strFilename = Path.GetFileName(myFile.FileName);
string strDir = "c:\\Inetpub\\wwwroot\\Estates\\Files\\" +
Session["usrName"].ToString();
Directory.CreateDirectory( strDir);
string
strRoute=MapPath("/Estates/Files/"+Session["usrName"].ToString()+"/"+strFile
name);
//string strFilename = Path.GetFileName(myFile.FileName);
//string strRoute=MapPath("/Estates/Files/"+strFilename );
WriteToFile(strRoute,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="Please, atach your file!";
return;
}
}
 
A

Alan Ferrandiz [MCT]

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 RegisterStartupScript 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="multipart/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.StringBuilder()
sb.Append("<script language=""JavaScript"">")
sb.Append(vbNewLine)
sb.Append(" function btnEnviarOnClick()")
sb.Append(vbNewLine)
sb.Append(" {")
sb.Append(vbNewLine)
sb.Append(" var archivo = document.Form1.txtFile.value;")
sb.Append(vbNewLine)
sb.Append(" if (archivo.length)")
sb.Append(vbNewLine)
sb.Append(" {")
sb.Append(vbNewLine)
sb.Append(" var extension = document.Form1.txtFile.value;")
sb.Append(vbNewLine)
sb.Append(" extension = extension.substring(extension.length-3,extension.length);")
sb.Append(vbNewLine)
sb.Append(" extension = extension.toLowerCase();")
sb.Append(vbNewLine)
sb.Append(" if(extension != 'doc')")
sb.Append(vbNewLine)
sb.Append(" {")
sb.Append(vbNewLine)
sb.Append(" window.alert(""Selecciono un archivo ."" + extension + "", por favor seleccione un archivo .doc"");")
sb.Append(vbNewLine)
sb.Append(" document.Form1.txtFile.focus();")
sb.Append(vbNewLine)
sb.Append(" return false;")
sb.Append(vbNewLine)
sb.Append(" }")
sb.Append(vbNewLine)
sb.Append(" else")
sb.Append(vbNewLine)
sb.Append(" return true;")
sb.Append(vbNewLine)
sb.Append(" }")
sb.Append(vbNewLine)
sb.Append(" else")
sb.Append(vbNewLine)
sb.Append(" {")
sb.Append(vbNewLine)
sb.Append(" window.alert(""Debe seleccionar un archivo"");")
sb.Append(vbNewLine)
sb.Append(" document.Form1.txtFile.focus();")
sb.Append(vbNewLine)
sb.Append(" return false;")
sb.Append(vbNewLine)
sb.Append(" }")
sb.Append(vbNewLine)
sb.Append(" }")
sb.Append(vbNewLine)
sb.Append("</script>")

Page.RegisterStartupScript("", sb.ToString)
btnEnviar.Attributes.Add( "onClick", "return btnEnviarOnClick();")




Viktor Popov said:
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.PostedFile != null) //HtmlInputFile filMyFile;
{
code
}
else
{
code
}

and with:
if(filMyFile.Value=="")
{
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(object sender, System.EventArgs e)
{
msg.Text="";
if(filMyFile.PostedFile != null)
{
try
{
// Get a reference to PostedFile object
HttpPostedFile myFile = filMyFile.PostedFile;
// Get size of uploaded file
int nFileLen = myFile.ContentLength;
if(myFile.ContentType.CompareTo("image/gif")==0)
{
// Allocate a buffer for reading of the file
byte[] myData = new byte[nFileLen];
// Read uploaded file from the Stream
myFile.InputStream.Read(myData, 0, nFileLen);
string strFilename = Path.GetFileName(myFile.FileName);
string strDir = "c:\\Inetpub\\wwwroot\\Estates\\Files\\" +
Session["usrName"].ToString();
Directory.CreateDirectory( strDir);
string
strRoute=MapPath("/Estates/Files/"+Session["usrName"].ToString()+"/"+strFile
name);
//string strFilename = Path.GetFileName(myFile.FileName);
//string strRoute=MapPath("/Estates/Files/"+strFilename );
WriteToFile(strRoute,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="Please, atach your file!";
return;
}
}
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top