上传图片代码。

前台是一个upload控件加一个按钮:
addline.aspx:
None.gif < tr >
None.gif    
< td  style ="width: 30px" > &nbsp; < asp:Label  ID ="Label9"  runat ="server"  Text ="Picture URL:" ></ asp:Label ></ td >
None.gif    
< td > &nbsp; < asp:TextBox  ID ="imgurl"
None.gif        runat
="server" > ../img/Noimg.jpg </ asp:TextBox >
None.gif    
< asp:Label  ID ="addimg"  runat ="server"  ForeColor ="Red" ></ asp:Label >< br  />
None.gif    
< asp:Label  ID ="Label10"  runat ="server"  Text ="Choose PIC:" ></ asp:Label >
None.gif    
< asp:FileUpload  ID ="imgurl1"  runat ="server"   />
None.gif        
< asp:Button
None.gif        
ID ="Button1"  runat ="server"  OnClick ="Button1_Click"  Text ="upload"   /></ td >
None.gif
</ tr >

addline.cs:
None.gif protected   void  Button1_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        Boolean fileOK 
= false;
InBlock.gif        String path 
= Server.MapPath("~/img/tour/");
InBlock.gif        String url 
= "../img/tour/" + imgurl1.FileName;
InBlock.gif        
if (imgurl1.HasFile)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            String fileExtension 
=
InBlock.gif                System.IO.Path.GetExtension(imgurl1.FileName).ToLower();
InBlock.gif            String[] allowedExtensions 
= 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif".gif"".png"".jpeg"".jpg" };
InBlock.gif            
for (int i = 0; i < allowedExtensions.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (fileExtension == allowedExtensions[i])
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    fileOK 
= true;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
if (fileOK)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif                imgurl1.PostedFile.SaveAs(path
InBlock.gif                    
+ imgurl1.FileName);
InBlock.gif                addimg.Text 
= "File uploaded!";
InBlock.gif                imgurl.Text 
= url;
InBlock.gif           
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            addimg.Text 
= "Cannot accept files of this type.";
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值