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