asp.net上傳檔案圖片的做法

來源:互聯網
上載者:User
建立一個web項目。把我下面貼的兩個檔案拿進去覆蓋同名檔案
webform1.aspx內,<>已經被替換為<>。請自己再替換回來。

webform1.aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication3.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="javascript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body ms_positioning="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋體"></FONT><INPUT id="File1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" type="file"
name="File1" runat="server">
<asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 40px" runat="server">Label</asp:Label>  
<INPUT style="Z-INDEX: 103; LEFT: 256px; POSITION: absolute; TOP: 8px" type="submit" value="Submit">
<asp:Image id="Image1" style="Z-INDEX: 104; LEFT: 32px; POSITION: absolute; TOP: 88px" runat="server"></asp:Image>
</form>
</FONT>
</body>
</HTML>

WebForm1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication3
{
///

/// Summary description for WebForm1.
///

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Image Image1;
protected System.Web.UI.HtmlControls.HtmlInputFile File1;

private void Page_Load(object sender, System.EventArgs e)
{
if(this.IsPostBack){
try
{
string filename=Server.MapPath("./")+"UpLoadFiles/"+Guid.NewGuid().ToString();
this.File1.PostedFile.SaveAs(filename);
this.Label1.Text=filename;
this.Image1.ImageUrl=filename;
}
catch(Exception ee)
{
this.Label1.Text=ee.Message;
}
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

///

/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.