• 上传图片代码。


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

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

                }

            }


            
    if (fileOK)
            
    {
                
                    imgurl1.PostedFile.SaveAs(path
                        
    + imgurl1.FileName);
                    addimg.Text 
    = "File uploaded!";
                    imgurl.Text 
    = url;
               
            }

            
    else
            
    {
                addimg.Text 
    = "Cannot accept files of this type.";
            }

        }
  • 相关阅读:
    top-cpu
    长连接&短连接分析
    HTTP协议详解(头报文)
    exit 与 return 区别
    TOP K 算法
    海量数据处理(面试题&总结)
    SpringBoot和VW-Crawler抓取csdn的文章
    maven项目-修复Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:build-helper-maven-plugin:1.8:add-resource (execution: add-resource, phase: generate-resources) pom.xml报错
    模拟ios应用加载页面
    HTML表格布局
  • 原文地址:https://www.cnblogs.com/ddr888/p/601174.html
Copyright © 2020-2023  润新知