---
title: ".NET添加多个附件的代码"
author: "wumingwei-123"
date: "2009-07-16T22:12:00Z"
tags: [".net", "JavaScript"]
slug: "net"
draft: false
lastmod: "2009-07-16T22:12:00Z"
id: 496854
url: "https://wumingwei.iteye.com/blog/496854"
---

多个附件一起上传 处理 入库等操作

在.aspx文件中的代码：

&lt;head runat="server"&gt;

&lt;script type="text/javascript" &gt;

function AddAttachments()

{

document.getElementById('attach').value = "继续添加附件";

tb = document.getElementById('attAchments');

newRow = tb.insertRow();

newRow.insertCell().innerHTML = "&lt;input name='File' size='50' type='file'&gt;&nbsp;&nbsp;&lt;input type=button value='删除'class='Button\_2'style='margin-top:-8px;' onclick='delFile(this.parentElement.parentElement.rowIndex)'&gt;";

}

function delFile(index)

{

document.getElementById('attAchments').deleteRow(index);

tb.rows.length &gt; 0?document.getElementById('attach').innerText = "继续添加附件":document.getElementById('attach').value = "添加附件";

}

&lt;/script&gt;

&lt;script type="text/javascript"&gt;

&lt;!--。。。。省略 其他代码--&gt;

&lt;/head&gt;

&lt;body&gt;

&lt;form id="form1" method="post" runat="server" target="\_self" enctype="multipart/form-data" &gt;

&lt;!--。。。。省略 其他代码--&gt;

&lt;table width="100%" cellpadding="0" cellspacing="0" border="0"&gt;&lt;tr style="display:none;"&gt;&lt;td style="height: 21px"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;

&lt;div style="text-align:right; width:457px; height: 13px; vertical-align:middle; padding-top:2px" class="tishi\_a" &gt;

&lt;input id="attach" type="button" style="font-family:宋体;font-size:9pt; height: 23px;" onclick="AddAttachments();" name="attach" value="添加附件" class="Button\_6" /&gt;&lt;/div&gt;

&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

&lt;!--。。。。省略 其他代码--&gt;

&lt;/form&gt;

&lt;/body&gt;

.cs代码文件的内容

//发送信息的附件操作

private bool saveAccess()

{

bool IsOK = false;

try

{

AccessoryModel accesMidel = new AccessoryModel();//这是一个附件所需的数据模型（下面有模型代码）

AccessoryBLL accesbll = new AccessoryBLL();//这是调用数据库的方法的逻辑层对象

for (int i = 0; i &lt; Request.Files.Count; i++)

{

if (Request.Files\[i].ContentLength &gt; 0)

{

fjid = Guid.NewGuid();

accesMidel.Id = fjid.ToString();

accesMidel.buffers = DBCOM.FileToByte.FilesToBytes(Request.Files\[i]);

accesMidel.Content = accesMidel.buffers;

accesMidel.Title = Request.Files\[i].FileName.Substring(Request.Files\[i].FileName.LastIndexOf("\\\\") + 1);

accesMidel.Msg\_Id = xxid.ToString ();//信息id

accesMidel.Suffix = Request.Files\[i].FileName.Substring(Request.Files\[i].FileName.LastIndexOf(".") + 1);

accesMidel.Size = float.Parse(Request.Files\[i].ContentLength.ToString());

accesMidel.Explain = text\_AcceExplain.Text.Trim();

accesMidel.UpLDate = System.DateTime.Now;

accesMidel.Remark = string.Empty;

accesMidel.Type = 1;//标示为政务信息的附件

if (accesbll.saveAccessory(accesMidel))

{

IsOK = true;

}

else

{

IsOK = false;

}

}

}

}

catch (Exception exx)

{

//Response.Write("&lt;script language='javascript'&gt;alert('" + + "');&lt;/" + "script&gt;");

LTP.Common.MessageBox.Show(this, exx.ToString());

}

return IsOK;

}

附模型代码：

namespace Model

{

public class AccessoryModel

{

private Byte\[] \_buffer;

private string \_Id;

private string \_Title;

private string \_Msg\_Id;

private Byte \[] \_Content;

private string \_Suffix;

private float \_Size;

private string \_Explain;

private DateTime \_UpLDate;

private string \_Remark;

private int \_Type;

public Byte\[] buffers

{

get { return \_buffer; }

set { \_buffer = value; }

}

/// &lt;summary&gt;

/// 附件ID

/// &lt;/summary&gt;

public string Id

{

get { return \_Id; }

set { \_Id = value; }

}

/// &lt;summary&gt;

/// 附件标题

/// &lt;/summary&gt;

public string Title

{

get { return \_Title; }

set { \_Title = value; }

}

/// &lt;summary&gt;

/// 回复

/// &lt;/summary&gt;

public string Msg\_Id

{

get { return \_Msg\_Id; }

set { \_Msg\_Id = value; }

}

/// &lt;summary&gt;

/// 后缀

/// &lt;/summary&gt;

public string Suffix

{

get { return \_Suffix; }

set { \_Suffix = value; }

}

/// &lt;summary&gt;

/// 附件内容

/// &lt;/summary&gt;

public Byte\[] Content

{

get { return \_Content; }

set { \_Content = value; }

}

/// &lt;summary&gt;

/// 附件大小

/// &lt;/summary&gt;

public float Size

{

get { return \_Size; }

set { \_Size = value; }

}

/// &lt;summary&gt;

/// 附件说明

/// &lt;/summary&gt;

public string Explain

{

get { return \_Explain; }

set { \_Explain = value; }

}

/// &lt;summary&gt;

/// 上传时间

/// &lt;/summary&gt;

public DateTime UpLDate

{

get { return \_UpLDate; }

set { \_UpLDate = value; }

}

/// &lt;summary&gt;

/// 备注

/// &lt;/summary&gt;

public string Remark

{

get { return \_Remark; }

set { \_Remark = value; }

}

/// &lt;summary&gt;

/// 附件类型为通知企业的附件

/// &lt;/summary&gt;

public int Type

{

get { return \_Type; }

set { \_Type = value; }

}

}

}

如有疑问请留言[或提问](http://www.lvtaostudio.com/ask/)我们将尽快答复

详情请见[http://www.lvtaostudio.com](http://www.lvtaostudio.com)