许松--永恒学习屋
努力并不一定会成功,但放弃一定会失败!
posts - 147,comments - 79,views - 19万
第一步:添加控件
<asp:Label ID="lblUploadErr" runat="server"></asp:Label>
<asp:FileUpload ID="fileDocument" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" 
            OnClientClick
="return CheckForTestFile();" onclick="btnUpload_Click" />

第二步:添加js代码
<script type="text/javascript">
        
////Trim the input text
        function Trim(input)
        
{
            
var lre = /^\s*/
            
var rre = /\s*$/
            input 
= input.replace(lre, ""); 
            input 
= input.replace(rre, ""); 
            
return input; 
        }

        
        
// filter the files before Uploading for text file only  
       function CheckForTestFile() 
       
{
            
var file = document.getElementById('<%=fileDocument.ClientID%>');
            
var fileName=file.value;        
            
//Checking for file browsed or not 
            if (Trim(fileName) =='' )
            
{
                alert(
"Please select a file to upload!!!");
                file.focus();
                
return false;
            }

     
           
//Setting the extension array for diff. type of text files 
           var extArray = new Array(".txt"".doc"".rtf"".pdf"".sxw"".odt"
                                    
".stw"".html"".htm"".sdw"".vor");       
     
           
//getting the file name
           while (fileName.indexOf("\\"!= -1)
             fileName 
= fileName.slice(fileName.indexOf("\\"+ 1);
     
           
//Getting the file extension                     
           var ext = fileName.slice(fileName.indexOf(".")).toLowerCase();
     
           
//matching extension with our given extensions.
           for (var i = 0; i < extArray.length; i++
           
{
             
if (extArray[i] == ext) 
             

               
return true;
             }

           }
  
             alert(
"Please only upload files that end in types:  " 
               
+ (extArray.join("  ")) + "\nPlease select a new "
               
+ "file to upload and submit again.");
               file.focus();
               
return false;                
       }

</script>
第三步:添加服务器端事件
protected void btnUpload_Click(object sender, EventArgs e)
        
{
            
try
            
{

                
//获取上载文件文件大小
                int intDocFileLength = fileDocument.PostedFile.ContentLength;

                
//获取客户端上的文件的完全限定名称
                string strPostedFileName = fileDocument.PostedFile.FileName;

                
//如果大于4MB
                if (intDocFileLength > 4096000)
                
{
                    lblUploadErr.Text 
= "上传文件大小大于4MB";
                    
return;
                }


                
if (strPostedFileName != String.Empty)
                
{
                    
//返回指定路径的扩展名,并转成小写
                    string strExtn = System.IO.Path.GetExtension(strPostedFileName).ToLower();

                    
if ((strExtn == ".txt"|| (strExtn == ".doc"|| (strExtn == ".rtf"|| (strExtn == ".pdf"||
                        (strExtn 
== ".sxw"|| (strExtn == ".odt"|| (strExtn == ".html"|| (strExtn == ".stw"||
                        (strExtn 
== ".htm"|| (strExtn == ".sdw"|| (strExtn == ".vor"))
                    
{
                        
//返回与服务器上的指定虚拟路径相对应的物理文件路径
                        string savePath = Server.MapPath(ConfigurationManager.AppSettings["UploadedPath"]) + "\\";

                        
//保存上传的文件内容
                        fileDocument.PostedFile.SaveAs(savePath + System.IO.Path.GetFileName(strPostedFileName));

                        lblUploadErr.Text 
= "document uploaded successfully";
                    }

                    
else
                    
{

                        lblUploadErr.Text 
= "Please upload a valid Document with the extenion in:<br>";
                        lblUploadErr.Text 
= ".txt,.doc,.rtf,.pdf,.sxw,.odt,.stw,.html,.htm,.sdw Or .vor";
                    }

                }

                
else
                
{

                    lblUploadErr.Text 
= "There is no file to Upload";
                }

            }

            
catch (System.Exception ex)
            
{
                lblUploadErr.Text 
= ex.Message;
            }

        }

第四步:Web.config
  <appSettings>
    <add key="UploadedPath" value="/"/>
  </appSettings>

Value值是上传到的虚拟路径,这里写了根目录
posted on   yongheng's blogs  阅读(657)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
< 2008年8月 >
27 28 29 30 31 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 1 2 3 4 5 6

点击右上角即可分享
微信分享提示