欢迎光临

木子屋

标题: C#保存文件到MongoDB示例

作者: dnawo

日期: 2012-08-21 11:24 AM

分类: Web编程

内容: MongoFileManger.cs：
复制内容到剪贴板 程序代码using System;
using System.Collections.Generic;
using System.IO;
using MongoDB.Driver;
using MongoDB.Driver.GridFS;
/// <summary>
///MongoFileManger 的摘要说明
/// </summary>
public class MongoFileManger
{
#region Fields
private MongoDatabase _database;
#endregion
#region Properties
public string ConnectionString { get; private set; }
public string DatabaseName { get; private set; }
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
public MongoFileManger()
: this("mongodb://localhost/?safe=true", "test")
{
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="connectionString"></param>
/// <param name="databaseName"></param>
public MongoFileManger(string connectionString, string databaseName)
{
ConnectionString = connectionString;
DatabaseName = databaseName;
_database = (MongoServer.Create(ConnectionString)).GetDatabase(DatabaseName);
}
#endregion
#region Methods
/// <summary>
/// Write
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public string Write(string path)
{
string filename = Guid.NewGuid().ToString();
MongoGridFS gridfs = new MongoGridFS(_database);
byte[] bytes = new byte[1024];
int length = 0;
using (MongoGridFSStream stream = gridfs.Open(filename, FileMode.Create))
{
using (FileStream fileStream = File.Open(path, FileMode.Open))
{
while ((length = fileStream.Read(bytes, 0, bytes.Length)) > 0)
{
stream.Write(bytes, 0, length);
}
}
}
return filename;
}
/// <summary>
/// Write
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public string Write(byte[] bytes)
{
string filename = Guid.NewGuid().ToString();
MongoGridFS gridfs = new MongoGridFS(_database);
using (MongoGridFSStream stream = gridfs.Open(filename, FileMode.Create))
{
stream.Write(bytes, 0, bytes.Length);
}
return filename;
}
/// <summary>
/// Read
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public byte[] Read(string filename)
{
MongoGridFS gridfs = new MongoGridFS(_database);
byte[] bytes;
using (MongoGridFSStream stream = gridfs.Open(filename, FileMode.Open))
{
bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
}
return bytes;
}
/// <summary>
/// Delete
/// </summary>
/// <param name="filename"></param>
public void Delete(string filename)
{
MongoGridFS gridfs = new MongoGridFS(_database);
gridfs.Delete(filename);
}
/// <summary>
/// FindAll
/// </summary>
/// <returns></returns>
public List<string> FindAll()
{
List<string> result = new List<string>();
MongoGridFS gridfs = new MongoGridFS(_database);
MongoCursor<MongoGridFSFileInfo> files = gridfs.FindAll();
foreach (MongoGridFSFileInfo item in files)
result.Add(item.Name);
return result;
}
#endregion
}
调用示例
复制内容到剪贴板 程序代码<%@ WebHandler Language="C#" Class="Default" %>
using System;
using System.Web;
using System.Collections.Generic;
public class Default : IHttpHandler {
public void ProcessRequest (HttpContext context) {
//context.Response.ContentType = "text/plain";
MongoFileManger file = new MongoFileManger();
string filename = file.Write(@"E:\Demo\WebSite1\test.gif");
byte[] bytes = file.Read(filename);
context.Response.BinaryWrite(bytes);
//MongoFileManger manager = new MongoFileManger();
//List<string> files = manager.FindAll();
//foreach (string item in files)
// context.Response.Write(item + "<br/>");
}

public bool IsReusable {
get {
return false;
}
}
}

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 5.310 ms

返回

标题: C#保存文件到MongoDB示例

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 5.310 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 5.451 ms

返回