如何在ASP.NET中上传文件 [英] How to Upload file in ASP.NET

查看:60
本文介绍了如何在ASP.NET中上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友

i希望在ASP.NET上传文件

i am使用以下代码。





< asp:DataList ID =dlistrunat =serverRepeatColumns =3>

< itemtemplate>

< asp:Image ID =img1runat =server

ImageUrl =''<%#Eval(Name,〜/ Images / {0})%> ;''

style =width:200px; height:200px;/>




<%#Eval (姓名,〜/ Images / {0})%>



和按钮点击



 受保护  void  Page_PreRender( object  sender,EventArgs e)
{
string upFolder = MapPath( 〜/ Images /);
DirectoryInfo dir = new DirectoryInfo(upFolder);
dlist.DataSource = dir.GetFiles();
dlist.DataBind();
}
受保护 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 (例外ee)
{
Response.Write ( 消息: + 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 ;
默认
返回 false ;
}
}


short 上传文件的方式...如果是,请帮助我..
请给出答案..
非常感谢。

解决方案

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 uploaded

Regular expression to validate file formats for .jpeg or .JPEG or .gif or .GIF or .png or .PNG

Re= /^(([a-zA-Z]:)|(\\{2}\w+)\


这篇关于如何在ASP.NET中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆