用file field控件
注意:表单的Enctype(编码类型)属性需设定为"multipart/form-data";需要将服务器端保存文件的目录设置为任何人可以存取
if((FileUp.PostedFile!=null)&&(FileUp.PostedFile.ContentLength >0))
{
...
try
{
FileUp.PostedFile.SaveAs("服务器上的目录");
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
}
参见
http://www.csdn.net/expert/topic/945/945606.xml?temp=.7109644
注意:表单的Enctype(编码类型)属性需设定为"multipart/form-data";需要将服务器端保存文件的目录设置为任何人可以存取
if((FileUp.PostedFile!=null)&&(FileUp.PostedFile.ContentLength >0))
{
...
try
{
FileUp.PostedFile.SaveAs("服务器上的目录");
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
}
参见
http://www.csdn.net/expert/topic/945/945606.xml?temp=.7109644
{
Stream imgStream;
int imgLen;
string imgName_value;
string imgContentType;
string imgUploadedName;
imgStream = UploadFile.PostedFile.InputStream;
imgLen = UploadFile.PostedFile.ContentLength;
imgUploadedName = UploadFile.PostedFile.FileName;
byte[] imgBinaryData=new byte[imgLen];
imgContentType = UploadFile.PostedFile.ContentType;
imgName_value = imgName.Value; try
{
if(imgName_value.Length < 1)
{
imgName_value = GetLastRightOf("\\",imgUploadedName );
}
}
catch(Exception myEx)
{
Response.Write(myEx.Message);
} int n = imgStream.Read(imgBinaryData, 0, imgLen);
int NumRowsAffected = MyDatabaseMethod(imgName_value, imgBinaryData, imgContentType);
if(NumRowsAffected > 0)
Response.Write( "<BR> uploaded image " );
else
Response.Write ( "<BR> an error occurred uploading the image.d " );
}
public string GetLastRightOf(string LookFor,string myString)
{
int StrPos;
StrPos = myString.LastIndexOf(LookFor);
return myString.Substring(StrPos + 1);
}
public int MyDatabaseMethod(string imgName,byte[] imgbin,string imgcontenttype)
{
SqlConnection connection = new SqlConnection(Application["Test_Conn"].ToString());
string SQL="INSERT INTO Image (img_name,img_data,img_contenttype) VALUES ( @img_name, @img_data,@img_contenttype )";
SqlCommand command=new SqlCommand ( SQL,connection );
SqlParameter param0=new SqlParameter ( "@img_name", SqlDbType.VarChar,50 );
param0.Value = imgName;
command.Parameters.Add( param0 ); SqlParameter param1=new SqlParameter ( "@img_data", SqlDbType.Image );
param1.Value = imgbin;
command.Parameters.Add( param1 );
SqlParameter param2 =new SqlParameter ( "@img_contenttype", SqlDbType.VarChar,50 );
param2.Value = imgcontenttype;
command.Parameters.Add( param2 );
connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();
return numRowsAffected;
}
}
}
http://www.csdn.net/develop/Read_Article.asp?Id=15001