文件上传及其注意事项

<%@ Page language="c#" Codebehind="PrepareAddProductByExcel.aspx.cs" AutoEventWireup="false" Inherits="wztj.page.product.PrepareAddProductByExcel" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>repareAddProductByExcel</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" enctype="multipart/form-data">
   <input type="file" id="File1" runat="server">
   <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 248px; POSITION: absolute; TOP: 160px" runat="server"
    Text="Button"></asp:Button>
   <asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 24px; POSITION: absolute; TOP: 168px" runat="server"
    Width="176px">Label</asp:Label>
  </form>
 </body>
</HTML>

------------------------------------------------------------------------------------------------------------------------------
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 wztj.page.product
{
 /// <summary>
 /// Summary description for repareAddProductByExcel.
 /// </summary>
 public class PrepareAddProductByExcel : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.HtmlControls.HtmlInputFile File1;
  protected System.Web.UI.WebControls.Button Button1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // Put user code to initialize the page here
  }

  #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);
  }
  
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {   
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void Button1_Click(object sender, System.EventArgs e) {
   if(this.File1.Value.Trim().Equals(""))
   {
    this.Label1.Text="上传文件不能为空";   
   }
   else if(this.File1.PostedFile==null)
   {
    this.Label1.Text="所指的文件不存在";
   }
   else{
    string FilePath = "C://Inetpub//wwwroot//wztj//excel//";
    string FileName = this.getFileNameByURL(this.File1.PostedFile.FileName);
    this.File1.PostedFile.SaveAs(FilePath+FileName);
   }
   
  }

  public string getFileNameByURL(string URL){
   int LastIndex = URL.LastIndexOf("//");
   string FileName = URL.Substring(LastIndex,URL.Length-LastIndex);
   return FileName;
  }
 }
}
-----------------------------------------------------------------------------------------------------------------------------
我用的是windows server 2003,把相应的目录的安全性里面,添加ASPNET用户
在web.config里面添加 <identity impersonate="true"/>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值