上传文件,图片代码

上传文件,图片代码 CS 类 

 

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;

namespace OA.Components
{
    
/// <summary>
    
/// Ducument 的摘要说明。
    
/// </summary>

    public class DocumentDB
    
{

        
//<summary>
        
//添加新的文档 AddDocument
        
//</summary>
        
//<return> DocumnetID</return>

        
public int AddDocument(string _Title,string _Content,int _UserID,string _DuUrl,string _Author,string _FromSource)
        
{
            Database db 
= new Database();
            SqlParameter [] param 
= {
                db.CreateInParam(
"@Title",SqlDbType.VarChar,200,_Title),
                db.CreateInParam(
"@Content",SqlDbType.Text,8000,_Content),
                db.CreateInParam(
"@UserID",SqlDbType.Int,4,_UserID),
                db.CreateInParam(
"@DocUrl",SqlDbType.VarChar,200,_DuUrl),
                db.CreateInParam(
"@Author",SqlDbType.VarChar,200,_Author),
                db.CreateInParam(
"@FromSource",SqlDbType.VarChar,200,_FromSource)
                                    }
;
            
return (db.RunProc("Pr_AddDocument",param));
        }

    }

}

 

应用

ASPX 页 HTML代码

 

<%@ Page language="c#" Codebehind="AddDocument.aspx.cs" AutoEventWireup="false" Inherits="OA.DesktopModules.Document.AddDocument" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>AddDocument</title>
        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        
<meta name="CODE_LANGUAGE" Content="C#">
        
<meta name="vs_defaultClientScript" content="JavaScript">
        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
        
<LINK href="../../Css/Default.css" type="text/css" rel="stylesheet">
    
</HEAD>
    
<body>
        
<form id="Form1" method="post" runat="server">
            
<div class="H1">上传文件:</div>
            
<DIV class="H3">文件主题:<INPUT type="text" id="Title" name="DocTitle" runat="server">
                
<asp:RequiredFieldValidator id="rvTitle" runat="server" ErrorMessage="请填写主题" Display="Dynamic" ControlToValidate="Title"></asp:RequiredFieldValidator></DIV>
            
<DIV class="H3">&nbsp;</DIV>
            
<DIV class="H3">文件注释:<INPUT id="Content" type="text" name="DocContent" runat="server"></DIV>
            
<DIV class="H3">&nbsp;</DIV>
            
<DIV class="H3"><FONT face="宋体">文件作者:<INPUT id="Author" type="text" name="txtAuthor" runat="server"></FONT></DIV>
            
<DIV class="H3"><FONT face="宋体"></FONT>&nbsp;</DIV>
            
<DIV class="H3"><FONT face="宋体">文件来源:<INPUT id="From" type="text" name="DocCatalog" runat="server"></FONT></DIV>
            
<DIV class="H3"><FONT face="宋体"></FONT>&nbsp;</DIV>
            
<DIV class="H3"><FONT face="宋体">文件路径:<INPUT id="FileDir" type="file" name="FileDir" runat="server"></FONT></DIV>
            
<DIV class="H3"><FONT face="宋体"></FONT>&nbsp;</DIV>
            
<DIV class="H3"><FONT face="宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT><FONT face="宋体">
                    
<asp:Button id="btAdd" runat="server" Text=" 提 交 "></asp:Button>&nbsp;&nbsp;
                    
<asp:Button id="btClear" runat="server" Text=" 返 回 "></asp:Button></FONT></DIV>
        
</form>
    
</body>
</HTML>

 

CS

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using OA.Components;

namespace OA.DesktopModules.Document
{
    
/// <summary>
    
/// AddDocument 的摘要说明。
    
/// </summary>

    public class AddDocument : System.Web.UI.Page
    
{
        
protected System.Web.UI.HtmlControls.HtmlInputText Title;
        
protected System.Web.UI.HtmlControls.HtmlInputText Content;
        
protected System.Web.UI.HtmlControls.HtmlInputText Author;
        
protected System.Web.UI.HtmlControls.HtmlInputText From;
        
protected System.Web.UI.HtmlControls.HtmlInputFile FileDir;
        
protected System.Web.UI.WebControls.Button btAdd;
        
protected System.Web.UI.WebControls.RequiredFieldValidator rvTitle;
        
protected System.Web.UI.WebControls.Button btClear;
        
private static string FILEDIR = "/DesktopModules/Document/FileLoad";
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            if (Session["UserID"== null)
            
{
                
//    SystemTools.Encrypt("请先登录!");
                Response.Redirect("~/home.aspx");
            }


        }


        
Web 窗体设计器生成的代码

        
private string FileUpload(HtmlInputFile inputFile) //上传文件
        {
            
string fileName="";
            
if ((inputFile.PostedFile.ContentLength > 0&&(inputFile.Value.ToString() != ""))
            
{
                
string fileTaiName =""//文件后缀
                fileTaiName = inputFile.PostedFile.FileName.Substring(inputFile.PostedFile.FileName.LastIndexOf("."));
                fileName 
= inputFile.PostedFile.FileName.Substring(
                    inputFile.PostedFile.FileName.LastIndexOf(
"/"),
                    inputFile.PostedFile.FileName.Length 
- inputFile.PostedFile.FileName.LastIndexOf("/"));
                
string fileTime = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
                                  DateTime.Now.Hour.ToString() 
+ DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString() +
                                  DateTime.Now.Millisecond.ToString();
                fileName 
= "/" + fileTime + GetRandomint() + fileName.Substring(fileName.IndexOf("."),fileName.Length - fileName.IndexOf("."));
             }


            
if (File.Exists(Server.MapPath(Request.ApplicationPath) + FILEDIR + fileName) == false)
            
{
                
try
                
{
                    
if(Directory.Exists(Server.MapPath(Request.ApplicationPath) + FILEDIR) == false )
                    Directory.CreateDirectory(Server.MapPath(Request.ApplicationPath) 
+ FILEDIR);
                    inputFile.PostedFile.SaveAs(Server.MapPath(Request.ApplicationPath) 
+ FILEDIR + fileName);
                }

                
catch (Exception ex)
                
{
                    
throw new Exception(ex.Message,ex);
                }

            }

            
if (fileName == ""){return (null);}
            
else {return (FILEDIR + fileName);}
        }


        
private string GetRandomint()
        
{
            Random random 
= new Random();
            
return (random.Next(10000).ToString());
        }


        
private void btAdd_Click(object sender, System.EventArgs e)
        
{
            
if (Title.Value.ToString().Trim().Length > 0)
            
{
                
string docUrl =  FileUpload(FileDir);
                DocumentDB doc 
= new DocumentDB();
                
int docID = doc.AddDocument(Title.Value.ToString(),Content.Value.ToString(),
                            Int32.Parse(Session[
"UserID"].ToString()),docUrl,Author.Value.ToString(),From.Value.ToString());
                
if (docID > 0)
                
{
                    SystemTools.AlertMsg(
"上传成功!");
                }

                
else
                
{
                    
if (File.Exists(Server.MapPath(Request.ApplicationPath)+docUrl) == true)
                    
{
                        File.Delete(docUrl);
                    }

                    SystemTools.AlertMsg(
"上传失败!");
                }


                
            }

            ClearInfo();
        }


        
private void ClearInfo()
        
{
            Title.Value 
= "";
            Content.Value 
="";
            Author.Value 
= "";
            From.Value 
= "";
        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值