FileUpload控件如何获取要上传文件的路径

本文将介绍如何利用JavaScript结合HTML元素实现文件上传功能,包括获取上传文件的路径、验证文件选择是否为空以及触发上传操作的过程。通过内联脚本函数检查文件路径,并在用户尝试上传文件前确保正确选择了文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BorrowPage.aspx.cs" Inherits="Borrow_BorrowPage" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>xxx上传</title>
        <!--获取文件上传地址-->
    <script type="text/javascript">
    function checkFile()
    {
        //判断浏览器类型
        var isIE = (document.all) ? true : false;
        var isIE7 = isIE && (navigator.userAgent.indexOf('MSIE 7.0') != -1);
        var isIE8 = isIE && (navigator.userAgent.indexOf('MSIE 8.0') != -1);
 
        var file=document.getElementById("UpLoadBorrow");
        var path=file.value;
        if(isIE7 || isIE8)
        {
            file.select();
            path=document.selection.createRange().text;
            document.selection.empty();
        }
       document.getElementById("txtFilePath").value=path;
    }
    function checkNull()
    {
        var path=document.getElementById("txtFilePath").value;
       if(path=="" ||path==null)
       {
        alert('请选择要上传的文件!');
        return false;
       }
       return true;
    }
    </script>
    <base target="_self"/>
</head>
<body style="">
    <form id="form1" runat="server">
    <div style="margin:0px auto 0px auto;">
        <asp:Image ID="Image1" runat="server" BorderStyle="Double" Width="750" ImageAlign="Middle" AlternateText="上传"/>
        <br />
        上传:<asp:FileUpload ID="UpLoadBorrow" runat="server" Height="22px" onchange="checkFile()" onkeydown="event.returnValue=false;" onpaste="return false" />
        <input type="hidden" id="txtFilePath" runat="server" />//这里建立一个隐藏域 用来存地址
        <asp:Button ID="btnUpload" runat="server" Text="上传" OnClientClick="return checkNull()" OnClick="btnUpLoad_Click"  />
        <asp:Button ID="btnBack" runat="server" Text="返回" onmousedown="window.close();" /></div>
    </form>
</body>
</html>

转载于:https://www.cnblogs.com/hqhouse/p/4230590.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值