2、附件上传到服务器及附件信息添加到数据库

本文介绍了如何在Asp.Net MVC中进行附件上传,并将附件信息存储到数据库。内容包括附件主表结构的设计,实现了附件列表展示,前端上传代码的编写,以及附件管理页面的展示效果。此外,还提供了示例代码和数据库下载链接。
摘要由CSDN通过智能技术生成


1、附件主表结构

CREATE TABLE [dbo].[Stm_Attach_Hd](
	[AttachmentKey] [INT] IDENTITY(1,1) NOT NULL,
	[AttachmentCode] [VARCHAR](200) NULL,
	[AttachmentName] [VARCHAR](200) NULL,
 CONSTRAINT [PK_Stm_Attachment] PRIMARY KEY CLUSTERED 
(
	[AttachmentKey] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

2、附件明细表结构

CREATE TABLE [dbo].[Stm_Attach_Dt](
	[AttachKey] [INT] IDENTITY(1,1) NOT NULL,
	[AttachmentKey] [INT] NULL,
	[ContentLength] [INT] NULL,
	[ContentType] [VARCHAR](500) NULL,
	[SourceFileName] [VARCHAR](500) NULL,
	[SourcePathName] [VARCHAR](500) NULL,
	[NewFileName] [VARCHAR](500) NULL,
	[NewPathName] [VARCHAR](500) NULL,
	[UserHostAddress] [VARCHAR](500) NULL,
	[UserHostName] [VARCHAR](500) NULL,
	[CreateTime] [DATETIME] NULL,
 CONSTRAINT [PK_Stm_Attach_Dt] PRIMARY KEY CLUSTERED 
(
	[AttachKey] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

3、附件上传时存储过程处理

USE [Hello]
GO
/****** Object:  StoredProcedure [dbo].[P_CreateAttachment]    Script Date: 07/11/2015 18:06:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[P_CreateAttachment](	
	@AttachmentKey INT,
	@ContentLength int,
	@ContentType varchar(20),
	@SourceFileName varchar(500),
	@SourcePathName varchar(500),
	@NewFileName varchar(500),
	@NewPathName varchar(500),
	@UserHostAddress varchar(500),
	@UserHostName  varchar(500)	
	)
AS
BEGIN
	DECLARE @AttachmentCode VARCHAR(200);
	DECLARE @AttachmentName VARCHAR(200);	
	
	IF @AttachmentKey<=0 
	BEGIN
	
	SET @AttachmentCode=CONVERT(varchar(100), GETDATE(), 21);
	SET @AttachmentName=CONVERT(varchar(100), GETDATE(), 21);
	INSERT INTO dbo.Stm_Attach_Hd
	        ( AttachmentCode, AttachmentName )
	VALUES  ( @AttachmentCode, @AttachmentName)  SET @AttachmentKey=@@IDENTITY;
	END;
	
	DECLARE @AttachKey INT;
	
	INSERT INTO dbo.Stm_Attach_Dt
	        ( AttachmentKey ,
	          ContentLength ,
	          ContentType ,
	          SourceFileName ,
	          SourcePathName ,
	          NewFileName ,
	          NewPathName ,
	          UserHostAddress ,
	          UserHostName ,
	          CreateTime
	        )
	VALUES  ( 
			@AttachmentKey , 
			@ContentLength,
			@ContentType,
			@SourceFileName,
			@SourcePathName,
			@NewFileName,
			@NewPathName,
			@UserHostAddress,
			@UserHostName,
			GETDATE() 
	        ) SET @AttachKey=@@IDENTITY;	
	        
	IF @AttachKey>0
	RETURN @AttachmentKey;
	ELSE
	RETURN 0;
	
END


4、列表页面代码

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content=&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值