单文件上传-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)' οnclick="javascript:ShowUploadFile('<%=txtFileUrl.ClientID %>');">Upload</a>&nbsp;<a  title='view this url' href='javascript:void(0)' οnclick="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="关闭" οnclick="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 = "没有文件";
        }
    }
}

转载于:https://www.cnblogs.com/xushop/articles/1576615.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值