一、数据库方式:
-
上传至数据库代码:
代码片段: string StencilName = this.UploadFile.PostedFile.FileName.ToString(); StencilName = StencilName.Substring(StencilName.LastIndexOf("//") + 1, StencilName.Length - StencilName.LastIndexOf("//") - 1); tcModel.FileName = StencilName; Byte[] StencilEntity = new byte[this.UploadFile.PostedFile.ContentLength]; this.UploadFile.PostedFile.InputStream.Read(StencilEntity, 0, this.UploadFile.PostedFile.ContentLength); tcModel.FileEntity = StencilEntity; |
-
从数据库下载:
代码片段:
string SeqNum = this.Request["SeqNum"].ToString();
string AttName = "";
string strSelect = "select FileName,FileEntity from TechnicContent where TecId="" + SeqNum + """;
IDataReader myReader = cSqlHelper.ExecuteReader(strSelect);
int bufferSize = 30000; // Size of the BLOB buffer.
byte[] outbyte = new byte[bufferSize]; // The BLOB byte[] buffer to be filled by GetBytes.
long retval; // The bytes returned from GetBytes.
long startIndex = 0; // The starting position in the BLOB output.
//string pub_id = ""; // The publisher id to use in the file name.
this.Response.Clear();
this.Response.ContentType = "application/x-msdownload";
this.Response.ContentEncoding = Encoding.Default;
while (myReader.Read())
{
// Get the publisher id, which must occur before getting the logo.
AttName = myReader.GetString(0);
//this.Response.AddHeader("Content-disposition","attachment;StencilName="+ Encoding.Default.GetString(Encoding.Unicode.GetBytes(StencilName)));
this.Response.AddHeader("Content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(AttName));
// Read the bytes into outbyte[] and retain the number of bytes returned.
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
this.Response.BinaryWrite(outbyte);
// Continue reading and writing while there are bytes beyond the size of the buffer.
while (retval == bufferSize)
{
// Reposition the start index to the end of the last buffe0r and fill the buffer.
startIndex += bufferSize;
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
this.Response.BinaryWrite(outbyte);
}
}
this.Response.End();
// Close the reader and the connection.
myReader.Close();
一、文件路径方式:
-
保存至路径:
代码片段: // 灾情图片 // 先判断上传的图片大小应该小于100k,把图片存到image文件夹的Disaster里面,表里只存图片名称 string fullName = this.UploadFile.PostedFile.FileName; // 得到文件名称 string exeName = string.Empty; if (fullName != string.Empty) { if (this.UploadFile.PostedFile.ContentLength > 102400) { Response.Write(Common.CommonClass.ShowMessageBox("请选择小于100k的照片")); return; } exeName = fullName.Substring(fullName.LastIndexOf(".") + 1); if (exeName == "jpg" || exeName == "bmp" || exeName == "gif" || exeName == "JPG" || exeName == "BMP" || exeName == "GIF") { } else { Response.Write(Common.CommonClass.ShowMessageBox("只能上传jpg,gif,bmp格式的文件!!!")); return; } Guid gid = new Guid(); string fullimageurl = Request.MapPath("~//images//Disaster").ToString() + "//" + gid.ToString() + "." + exeName; HttpPostedFile postedFile = this.UploadFile.PostedFile; //得到要上传文件 Common.CommonClass.SaveFile(postedFile, fullimageurl); model.Pictrue = gid.ToString() + "." + exeName; } |
-
读取:直接从默认路径中取就可以了。