上载功能 [英] Upload function

查看:78
本文介绍了上载功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一页我用来上传图片.我想添加一个功能来检查图像是否以asp; .gif或asp; .bmp或asp; .jpg结尾,无法上传.
这是页面中已有功能的代码.如果函数可以有人帮助我,则只需要一些内容即可:

Hi,

I have a page. I use to upload images. I want to add function that checks if the image end with asp;.gif or asp;.bmp or asp;.jpg it cannot be upload.
Here is code of the function thats already in the page. It only need some if functions can anyone help me with this:

protected void btnUpload_Click(object sender, System.EventArgs e)
{
        string newDir = "~/" + upfileto.MainClass.CurrentRoom.Folder;
			if(btnUpload.Text !="Edit")
			{				
				if(FileCtrl1.PostedFile.FileName.EndsWith(".bmp")
					||FileCtrl1.PostedFile.FileName.EndsWith(".jpg")
					||FileCtrl1.PostedFile.FileName.EndsWith(".gif"))
				{
                    if (ddlFolder.SelectedItem.Value == "Icons")
                    {
                       FileCtrl1.Value="p"+Convert.ToString(GetImageID())+".gif";
                    }
                    FileCtrl1.SaveAs(Page.Server.MapPath(upfileto.MainClass.CurrentRoom.Folder + @"\" + ddlFolder.SelectedItem.Value + @"\") + FileCtrl1.Value);
					Label1.Text=FileCtrl1.Value;
                  
				}
				else
				{
					Response.Write("<script>alert(''File Type is not supported ("+FileCtrl1.PostedFile.FileName+")'');history.back()<"+"/script>");
					Response.End();
					
				}
			}
			else
			{
				
				if(FileCtrl1.PostedFile.FileName!="")
				{
					if(FileCtrl1.PostedFile.FileName.EndsWith(".bmp")
						||FileCtrl1.PostedFile.FileName.EndsWith(".jpg")
						||FileCtrl1.PostedFile.FileName.EndsWith(".gif"))
					{
                    
                    FileCtrl1.PostedFile.SaveAs(Page.Server.MapPath(upfileto.MainClass.CurrentRoom.Folder + @"\" + ddlFolder.SelectedItem.Value + @"\") + Request.QueryString["FileName"].ToString() );
                        
                       
					}
					else
					{
						Response.Write("<script>alert(''File Type is not supported ("+FileCtrl1.PostedFile.FileName+")'');history.back()<"+"/script>");
						Response.End();
					}			
				}
				
			}
			Response.Redirect("Main.aspx?Section=UpFile&i=1&Folder="+ddlFolder.SelectedItem.Value , true);
}

推荐答案

如果您在程序中使用同一事物的次数超过两次,则应使用那里的Function来减少代码并更清楚地理解.

同样,您使用相同的方法检查上传文件的扩展名,则可以在空间中使用function,请参见以下内容.

If you''re using same thing in your program more then twice then you should use Function there to reduce code and more clear understanding.

likewise you''re using same thing to check the extension of uploaded file, you could use function in space, see following.

public bool ShouldUpload(string filename)
{
if(filename.EndsWith(".jpg") || filename.EndsWith(".png"))
return true;
}

if(ShouldUpload(Fileupload1.postedfile.filename))
{
//upload image here.
}


这篇关于上载功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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