Asp文件上传需要只允许PDF文件从侧面 [英] Asp fileupload need to allow only PDF file from clent side

查看:94
本文介绍了Asp文件上传需要只允许PDF文件从侧面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i有一个fileupload控件需要从客户端验证只接受pdf。

如果我尝试上传其他文件除了pdf应该提醒并需要删除选定的路径



请给我解决方案。



< pre lang =c#>< asp:FileUpload ID = FileUplod1 runat = server高度= 18px onclientclick = CheckFile(this); /> ;







 函数 CheckFile(Cntrl){
var file = document 。的getElementById(Cntrl.name);
var len = file.value.length;
var ext = file.value;
if (ext.substr(len - 3 ,len)!= pdf
{
alert( 请选择doc或pdf文件);
return false ;
}
}

解决方案

访问:

http://www.aspsnippets.com/Articles/ASPNet-FileUpload-File-Extension-Validation.aspx [ ^ ]


我有运行示例并尝试提供所需信息。请使用以下代码



 <   asp:asyncfileupload     onuploadedcomplete   =  FileUploadComplete    xmlns:asp   = #unknown >  
runat =serverID =AsyncFileUploadImageWidth =400pxUploaderStyle =Modern
CompleteBackColor =WhiteUploadingBackColor =#0A7AC5ThrobberID =imgloading
OnClientUploadError =uploadErrorCssClass =fileuploadTabIndex =6OnClientUploadStarted =uploadImageStart/> < / asp:asyncfileupload >



  function  uploadImageStart(sender,args){
var filename = args.get_fileName();

var filext = filename.substring(filename.lastIndexOf( )+ 1 );
filext = filext.toLowerCase();
if (filext == pdf ){
return true ;
}
其他 {
var err = new 错误();
err.name = ' 填充外部错误';
err.message = ' 无效文件。只允许.pdf文件';
throw (错误);
return false ;
}
}

function uploadError(sender,args){
alert(args.get_errorMessage( ));

}





FileUploadComplete - 客户端后面的代码


javascript





< script type =   text / javascript> 
var validFilesTypes = [ bmp gif png jpg jpeg doc xls];
function ValidateFile()
{
var file = document .getElementById( <%= FileUpload1.ClientID%> ;);
var label = document .getElementById( <%= Label1.ClientID%>);
var path = file.value;
var ext = path.substring(path.lastIndexOf( )+ 1,path.length).toLowerCase();
var isValidFile = false ;
for var i = 0 ; i< validfilestypes.length;> {
if (ext == validFilesTypes [i])
{
isValidFile = true ;
break ;
}
}
if (!isValidFile)
{
label.style.color = red;
label.innerHTML = 文件无效。请上传一个文件,其中包含 +
扩展名:\ n \ n + validFilesTypes.join( );
}
return isV alidFile;
}
< / script>< / script>









代码背后的文件

  protected   void  btnUpload_Click( object  sender,EventArgs e)
{
string [] validFileTypes = { bmp gif png jpg jpeg doc xls};
string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
bool isValidFile = false ;
for int i = 0 ; i < validFileTypes.Length; i ++)
{
if (ext == + validFileTypes [i])
{
isValidFile = true ;
break ;
}
}
如果(!isValidFile)
{
Label1.ForeColor = System.Drawing。红色;
Label1.Text = 无效文件。请上传扩展名为 + $ b的文件$ b string .Join( ,validFileTypes);
}
else
{
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Text = 文件已成功上传。;
}
}


Hello all,

i have one fileupload control need to validate from client side accept only pdf.
if i try to upload other file except pdf should give alert and need to remove selected path

please give me solution.

<asp:FileUpload ID="FileUplod1" runat="server" Height="18px" onclientclick="CheckFile(this);" />




function CheckFile(Cntrl) {
        var file = document.getElementById(Cntrl.name);
        var len = file.value.length;
        var ext = file.value;
        if (ext.substr(len - 3, len) != "pdf")
        {
            alert("Please select a doc or pdf file ");
            return false;
        }
    }

解决方案

Visit Here:
http://www.aspsnippets.com/Articles/ASPNet-FileUpload-File-Extension-Validation.aspx[^]


I have running example and tried provide required info. please use below code

<asp:asyncfileupload onuploadedcomplete="FileUploadComplete" xmlns:asp="#unknown">
                                    runat="server" ID="AsyncFileUploadImage" Width="400px" UploaderStyle="Modern"
                                    CompleteBackColor="White" UploadingBackColor="#0A7AC5" ThrobberID="imgloading"
                                    OnClientUploadError="uploadError" CssClass="fileupload" TabIndex="6" OnClientUploadStarted="uploadImageStart" /></asp:asyncfileupload>


function uploadImageStart(sender, args) {
    var filename = args.get_fileName();

    var filext = filename.substring(filename.lastIndexOf(".") + 1);
    filext = filext.toLowerCase();
    if (filext == "pdf") {
        return true;
    }
    else {
        var err = new Error();
        err.name = 'Fill Ext Error';
        err.message = 'Not valid file. Only .pdf files allowed';
        throw (err);
        return false;
    }
}

function uploadError(sender, args) {
    alert(args.get_errorMessage());

}



FileUploadComplete - code behind


for client side javascript


<script type="text/javascript">
    var validFilesTypes=["bmp","gif","png","jpg","jpeg","doc","xls"];
    function ValidateFile()
    {
      var file = document.getElementById("<%=FileUpload1.ClientID%>");
      var label = document.getElementById("<%=Label1.ClientID%>");
      var path = file.value;
      var ext=path.substring(path.lastIndexOf(".")+1,path.length).toLowerCase();
      var isValidFile = false;
      for (var i=0; i<validfilestypes.length;>      {
        if (ext==validFilesTypes[i])
        {
            isValidFile=true;
            break;
        }
      }
      if (!isValidFile)
      {
        label.style.color="red";
        label.innerHTML="Invalid File. Please upload a File with" +
         " extension:\n\n"+validFilesTypes.join(", ");
      }
      return isValidFile;
     }
</script></script>





code behind file

protected void btnUpload_Click(object sender, EventArgs e)
{
    string[] validFileTypes={"bmp","gif","png","jpg","jpeg","doc","xls"};
    string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
    bool isValidFile = false;
    for (int i = 0; i < validFileTypes.Length; i++)
    {
        if (ext == "." + validFileTypes[i] )
        {
            isValidFile = true;
            break;
        }
    }
    if (!isValidFile)
    {
        Label1.ForeColor = System.Drawing.Color.Red;
        Label1.Text = "Invalid File. Please upload a File with extension " +
                       string.Join(",", validFileTypes);
    }
    else
    {
        Label1.ForeColor = System.Drawing.Color.Green;
        Label1.Text = "File uploaded successfully.";
    }
}


这篇关于Asp文件上传需要只允许PDF文件从侧面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆