更新时间:2023-02-15 19:17:38
http ://www.aspdotnet-suresh.com/2011/01/how-to-insert-images-into-database-and.html [ ^ ]
http:// www。 aspdotnet-suresh.com/2011/03/how-to-save-images-into-folder-and.html [ ^ ]
http://www.aspdotnet-suresh.com/2012/02/saveupload-files-in-folder-and-download.html [ ^ ]
嗨。这段代码可以帮到你。我不测试他们! ;)
用于保存文件使用:FileUpload1.SaveAs(Server.MapPath(Books /)+ FileUpload1.FileName);
用于文件类型检查的
使用这个返回答案(看起来更好切换!):if(
(System.IO.Path.GetExtension(FileUpload1.FileName)==.jpg)||(System.IO.Path.GetExtension(FileUpload1.FileName)==)。 jpeg)||(System.IO.Path.GetExtension(FileUpload1.FileName)==。bmp)||(System.IO.Path.GetExtension(FileUpload1.FileName)==。gif)||( System.IO.Path.GetExtension(FileUpload1.FileName)==.ico)||(System.IO.Path.GetExtension(FileUpload1.FileName)==。png)
)
{some code}
else
{some code}
if (FileUpload1.HasFile)
{
string name = Path.GetFileName(FileUpload1.PostedFile 。文件名);
string location = Server.MapPath( 〜/ docs / + name);
FileUpload1.SaveAs(location);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = insert into upload(FileName)values(@FileName);
cmd.Parameters.AddWithValue( @ FileName,name);
cmd.Connection = con;
con.Open();
int result = cmd.ExecuteNonQuery();
if (结果> 0 )
lblMessage.Visible = true ;
lblMessage.Text = 文件已保存;
cmd.Dispose();
con.Close();
使用自定义验证控件来控制可上传的文件正则表达式 验证文件格式 for .jpeg 或 .JPEG 或 .gif 或 .GIF 或 .png 或 .PNG
Re = / ^(([a-zA-Z] :) |(\\ { 2 } \w +)\
Hello Friends
i want to upload file in ASP.NET
i am using the following code.
<asp:DataList ID="dlist" runat="server" RepeatColumns="3">
<itemtemplate>
<asp:Image ID="img1" runat="server"
ImageUrl=''<%# Eval("Name", "~/Images/{0}") %>''
style="width:200px;height:200px;"/>
<%# Eval("Name", "~/Images/{0}") %>
and on button click
protected void Page_PreRender(object sender, EventArgs e)
{
string upFolder = MapPath("~/Images/");
DirectoryInfo dir = new DirectoryInfo(upFolder);
dlist.DataSource = dir.GetFiles();
dlist.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (FileUpload1.HasFile)
{
if (CheckFileType(FileUpload1.FileName))
{
String filePath = "~/Images/" + FileUpload1.FileName;
FileUpload1.SaveAs(Server.MapPath(filePath));
}
}
}
catch (Exception ee)
{
Response.Write("Message : " + ee.Message);
Response.Write(ee.StackTrace);
}
}
bool CheckFileType(string fileName)
{
string ext = Path.GetExtension(fileName);
switch (ext.ToLower())
{
case ".gif":
return true;
case ".png":
return true;
case ".jpg":
return true;
case ".jpeg":
return true;
default:
return false;
}
}
is there any short way to upload the file ...IF yes then please help me..
Please give answer..
Thanks a lot.
http://www.aspdotnet-suresh.com/2011/01/how-to-insert-images-into-database-and.html[^]
http://www.aspdotnet-suresh.com/2011/03/how-to-save-images-into-folder-and.html[^]
http://www.aspdotnet-suresh.com/2012/02/saveupload-files-in-folder-and-download.html[^]
Hi . this code may help you . i don''t test them ! ;)
for save file use this :FileUpload1.SaveAs(Server.MapPath("Books/") +FileUpload1.FileName);
for file-type check use this that return the answer ( seem better that switch!) :if( (System.IO.Path.GetExtension(FileUpload1.FileName) == ".jpg") || (System.IO.Path.GetExtension(FileUpload1.FileName) == ".jpeg") || (System.IO.Path.GetExtension(FileUpload1.FileName) == ".bmp") || (System.IO.Path.GetExtension(FileUpload1.FileName) == ".gif") || (System.IO.Path.GetExtension(FileUpload1.FileName) == ".ico") || (System.IO.Path.GetExtension(FileUpload1.FileName) == ".png") ) {some code} else {some code}
if (FileUpload1.HasFile) { string name = Path.GetFileName(FileUpload1.PostedFile.FileName); string location = Server.MapPath("~/docs/" + name); FileUpload1.SaveAs(location); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "insert into upload (FileName) values (@FileName)"; cmd.Parameters.AddWithValue("@FileName", name); cmd.Connection = con; con.Open(); int result = cmd.ExecuteNonQuery(); if (result > 0) lblMessage.Visible = true; lblMessage.Text = "File saved"; cmd.Dispose(); con.Close();
use custom validation control to control the documents that can be uploadedRegular expression to validate file formats for .jpeg or .JPEG or .gif or .GIF or .png or .PNG Re= /^(([a-zA-Z]:)|(\\{2}\w+)\