单文件上传-asp.net版本

1.web.config
appSettings配置节
<add key="UploadFileUrl" value="http://file.mydomain.com/" />
<add key="UploadFilePath" value="E:\file.mydomain.com\uploadfile\"/>

2.上传控件
2.1 UpLoadFile.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UpLoadFile.ascx.cs" Inherits="Controls_UpLoadFile" %>
<script language="javascript" >
    function ShowUploadFile(fileUrlTextBox){
     if(fileUrlTextBox != ""){
         var currentFileUrlTextBox = document.all[fileUrlTextBox];
         var result=window.showModalDialog("/UploadFileRequest.aspx?R="+Math.random());
         if(result == "") return;
   if(typeof(result)=="undefined") return;
   currentFileUrlTextBox.value = result;
     }
 }

 function viewUploadURL(fileUrlTextBox){
     if(fileUrlTextBox != ""){
         var currentFileUrlTextBox = document.all[fileUrlTextBox];
  if(currentFileUrlTextBox.value != "")
   window.open(currentFileUrlTextBox.value);
     }
 }
</script>

<asp:TextBox ID="txtFileUrl" runat="server" TextMode="SingleLine" Wrap="false" Columns="50" CssClass="uploadfilesCtrl" />&nbsp;<a title="Upload File" href='javascript:void(0)' onclick="javascript:ShowUploadFile('<%=txtFileUrl.ClientID %>');">Upload</a>&nbsp;<a  title='view this url' href='javascript:void(0)' onclick="javascript:viewUploadURL('<%=txtFileUrl.ClientID %>');" >View</a>

2.2 UpLoadFile.ascx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Controls_UpLoadFile : System.Web.UI.UserControl
{
    public string Text
    {
        get { return this.txtFileUrl.Text; }
        set { this.txtFileUrl.Text = value; }
    }

    public int Columns
    {
        get { return this.txtFileUrl.Columns; }
        set { this.txtFileUrl.Columns = value; }
    }
}


3.打开的上传页面
3.1 UploadFileRequest.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UploadFileRequest.aspx.cs"
    Inherits="UploadFileRequest" %>

<html>
<head>
    <title>Upload File</title>
    <base target="_self" />
</head>
<body leftmargin="0" rightmargin="0">

    <script language="javascript">
 window.onunload = Go;
 function Go(){
  window.returnValue = document.all.Hidden1.value;
 }
    </script>

    <form id="form1" runat="server">
        <div style="margin: 5px; text-align: center; width: 100%; padding: 5px;">
            <asp:FileUpload ID="FileUpload1" runat="server" Width="400px" /><br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" />
            <input id="Button2"  type="button" runat="server"  value="关闭" onclick="javascript:window.returnValue = Hidden1.value;window.close();" /><br />
            <br />
            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
            <input id="Hidden1" type="hidden" runat="server" />
        </div>
    </form>
</body>
</html>

3.2 UploadFileRequest.aspx。cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class UploadFileRequest : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (this.FileUpload1.FileName.Trim() != "")
        {
            string rootUrl = ConfigurationManager.AppSettings["UploadFileUrl"];
            string rootDir = ConfigurationManager.AppSettings["UploadFilePath"];
            string maxId = System.Guid.NewGuid().ToString();
            string fileName = maxId + "_" + this.FileUpload1.FileName;
            string orgName = "MyDomain";
            System.IO.Directory.CreateDirectory(rootDir + orgName.Trim() + "\\");
            this.FileUpload1.SaveAs(rootDir + orgName.Trim() + "\\" + fileName);

            string path = rootDir + orgName.Trim() + "\\" + fileName;                       
            string returnUrl = rootUrl + orgName.Trim() + "/" + fileName;
            this.Label1.Text = ("文件保存在") + returnUrl;
            this.Hidden1.Value = returnUrl;
        }
        else
        {
            this.Label1.Text = "没有文件";
        }
    }
}

posted @ 2009-09-29 21:28  XuShop  阅读(234)  评论(0)    收藏  举报