当前位置导航:炫浪网>>网络学院>>网页制作>>XML/WebService教程

图片上传代码示例

using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;
using KunslyNetDll.Common;
using HunMSM.include;

namespace HunMSM.photo
{
    
/// <summary>
    
/// 图片上传。
    
/// </summary>

    public partial class _upphoto : System.Web.UI.Page
    
{
        
public string sPath = "";
        
public string extension;
        
public string path;
        
public string fileName;
        
public string smobile;
        
protected void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            AddControl();
            
if (Session["UserMobile"== null)            
                Response.Write(
"<script language=javascript>alert('请先登录,才允许上传图片');history.back(-1)</script>");
            
else
                smobile 
= Session["UserMobile"].ToString().Trim();
            
if(!this.IsPostBack)
            
{
                DropDownPhotoType();
                DropDownPhotoColl();
            }

        }


        
void AddControl()    

        
Web 窗体设计器生成的代码

        
protected void Button1_Click(object sender, System.EventArgs e)
        
{
            
int ncount = GetRecordCount();
            
if (ncount == 0)
            
{
                KunslyWebPage.ShowMessage(
this,"请先创建相册,才允许上传图片");
                
return;
            }

            
//    Response.Write("<script language=javascript>alert('请先创建相册,才允许上传图片');history.back(-1)</script>");
            
//else
            
//{
            if(UploadFile.PostedFile.FileName.Trim() == ""
            

                
//Response.Write("<script language=javascript>alert('老大,没文件怎么传啊');history.back(-1)</script>");
                KunslyWebPage.ShowMessage(this,"请选择文件");
                
return;
            }
 
            
//    else
            
//    {
            if (txtMessage.Text.Trim() == "")
            
{
                KunslyWebPage.ShowMessage(
this,"请输入简介");
                
return;            
            }

                
//Response.Write("<script language=javascript>alert('老大,请输入简介');history.back(-1)</script>");
            extension = Path.GetExtension(UploadFile.PostedFile.FileName).ToUpper(); 
            
            
if (extension != ".JPG" && extension != ".GIF")
            
{
                KunslyWebPage.ShowMessage(
this,"非法文件,不允许上传,只允许上传.jpg?http://www.xvna.com或.gif?http://www.xvna.com文件");
                
return;            
            }

            
//Response.Write("<script language=javascript>alert('非法文件,不允许上传');history.back(-1)</script>");
            if (UploadFile.PostedFile.ContentLength >= 1000000//大于1M不允许上传
            {
                KunslyWebPage.ShowMessage(
this,"文件过大,不能上传,单个文件最大不超过1M");
                
return;            
            }

            fileName 
= DateTime.Now.ToString("yyyyMMddhhmmss"); 
            path 
= Server.MapPath(""+ "/upfile/" + fileName + extension; 
            updata();
        }
    
                
                    
            
        


        
void updata()


        
void DropDownPhotoType()

        
void DropDownPhotoColl()

        
int GetRecordCount() 



    }

}

相关内容
赞助商链接