zoukankan      html  css  js  c++  java
  • Ext.net中如何上传文件

    今天在使用ext.net的UploadField控件想上传文件时,发现examples.ext.net官网上的例子写的不是很详细。于是通过网上找资料,结合asp.net的文件上传的方法,终于实现了图片的上传功能。以下就是实现的代码,供大家参考!
    首先在.aspx文件中插入一个文件上传的控件:

    <ext:FileUploadField ID="UploadFile" runat="server" FieldLabel="附件上传" ButtonText="浏览..."/>

    然后是.cs文件中实现上传的具体代码:

                string UploadFile ="";
    if (this.UploadFile.HasFile)
    {
    UploadFile = this.UploadFile.PostedFile.FileName.ToString();
    int FileSize=Int32.Parse(this.UploadFile.PostedFile.ContentLength.ToString());
    if (FileSize > 5 * 1024 *1024)
    {
    X.Msg.Alert("提示信息", "上传文件过大!").Show();
    return;
    }
    string strFileName = Path.GetExtension(this.UploadFile.PostedFile.FileName).ToUpper();//获取文件后缀
    if (!(strFileName == ".BMP" || strFileName == ".GIF" || strFileName == ".JPG"))
    {
    X.Msg.Alert("提示信息", "文件格式不正确!").Show();
    return;
    }

    Random ran = new Random();
    string sNewName = DateTime.Now.ToString(@"yyyyMMddHHmmss") + ran.Next(100, 999)
                          + Path.GetExtension(this.UploadFile.PostedFile.FileName);
    string strPath = Server.MapPath("~/FileUpload/" + sNewName);
    if (!Directory.Exists(Path.GetDirectoryName(strPath)))
    {
    Directory.CreateDirectory(Path.GetDirectoryName(strPath));
    }
    this.UploadFile.PostedFile.SaveAs(strPath);
    }

    通过简单的操作,就是给上传的图片重新命名,并且保存到要保存的文件夹中。   


  • 相关阅读:
    QuickFlash
    第五课:类的封装性 M
    第六课:构造方法、匿名对象 M
    第四课:面向对象(基础) M
    第八课:引用传递 M
    第七课:String类 M
    第二课:数组 M
    第三课:方法 M
    ASP .Net的应用程序域(The Application Domain)
    js 解数独程序
  • 原文地址:https://www.cnblogs.com/jianglan/p/2414167.html
Copyright © 2011-2022 走看看