您的位置:首页 > 移动开发 > Objective-C

控制上传文件大小类型

2009-08-11 16:27 381 查看

控制类型

服务器端:
如下面上传图片的例子。
首先申明使用命名空间。using System.IO;

在设计页面拖进一个input(File)控件,并把它作为服务器控件运行。其ID为myFile;然后拖进一个button,给button的单击时间添加如下代码:

[codes=CSharp]

protected void submit_Click(object sender, EventArgs e)

    {

        string phName = this.txtName.Text;

        string phType = this.ddlType.SelectedValue; if
(this.myFile.PostedFile != null)

        {

            string photoName1 = myFile.PostedFile.FileName;
//获取初始文件名

            int i =
photoName1.LastIndexOf("."); //取得文件名中最后一个"."的索引

            string newext =
photoName1.Substring(i); //获取文件扩展名

            if (newext != ".gif"
&& newext != ".jpg"&&newext!=".jpeg"
&& newext != ".bmp" && newext != ".png")

                {

                   
Response.Write("文件格式不正确!");

                   
Response.End();

                }

       

                   DateTime
now = DateTime.Now; //获取系统时间

            string photoName2 =
now.Millisecond.ToString() + "_" +
myFile.PostedFile.ContentLength.ToString() + newext; //重新为文件命名,时间毫秒部分+文件大小+扩展名

           
myFile.PostedFile.SaveAs(Server.MapPath("photos//" + photoName2)); //
保存文件到路径,用Server.MapPath()取当前文件的绝对目录.在asp.net里"/"必须用"//"代替

        }

}

[/codes]

文件大小

  //、、上载文件
   
protected void
Buttonup_Click(object sender, EventArgs e)
   
{
       
string filepath = Server.MapPath("./") + "file";
       
HttpFileCollection HFC=Request.Files;
       
HttpPostedFile UserHPF = HFC[0];
       
try
       
{
            if
(UserHPF.ContentLength <=0)
            {
                Label1.Text = "文件为空!";
            }
            else
if (UserHPF.ContentLength / (1024*1024)> 1)
            {
               
                Label1.Text = "文件大于1M!";
               
           }
            else
            {
     UserHPF.SaveAs(filepath + "//" + System.IO.Path.GetFileName(UserHPF.FileName));
                Label1.Text = "上传成功!";
            }
                    
              
           
          
       
}
       
catch
       
{
            Label1.Text = "上传失败!";
       
}
       
addlistbox1();
   
}
   
protected void
LinkButton11_Click(object sender, EventArgs e)
   
{
   
}
   
protected void
TextBox1_TextChanged(object sender, EventArgs e)
   
{
       

}
例二:
if (this.FileUpload1.HasFile)

        

{

            int MaxLength = 1024 * 1024;//最大为1M

            string name = this.FileUpload1.FileName;//获取文件的名称如:panjun.doc panjun.gif

            string type = name.Substring(name.LastIndexOf(".") + 1).ToLower();//获取文件的类型

            if (this.FileUpload1.PostedFile.ContentLength > MaxLength)//限定上传大小为1MB

            

{

               Response.Write("<script>alert('上传文件的大小超过了1MB的最大容量!请压缩后再上传!')</script>");

                return;

            }

            if (type == "jpg" || type == "bmp" || type == "gif" || type == "png")

            

{

                string filepath = MapPath("../upload/img/" + name);

                if (!File.Exists(filepath))

                

{

                    this.FileUpload1.SaveAs(filepath);//这个是主要的完成上传的代码

                }

                else

                

{

                    Response.Write("<script>alert('文件已存在,请重命名后再上传!')</script>");

                    return;

                }

            }

            else

            

{

                Response.Write("<script>alert('你选择的文件格式不符合要求!')</script>");

                return;

            }

        }

        else

        

{

            Response.Write("<script>alert('请选择一个图片文件!')</script>");

            return;

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息