场景:我最近在我的 ASP.NET MVC 应用程序中添加了一个组件,允许他们将文件上传到数据库中。由于这些文件平均超过 2MB,我选择使用 FILESTREAM。我保存HttpPostedFileBase
到一个临时文件,做一些业务逻辑,然后上传文件。上传后,用户被重定向到查看文件的页面,内嵌在浏览器中。以下是相关的上传代码:
var DocContext = new DocumentEntities();
var dbFile = new File
{
DocumentID = Guid.NewGuid(),
Name = fileName,
Type = file.ContentType
};
DocContext.Document.Add(dbFile);
DocContext.SaveChanges();
using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
using (var sqlFS = dbFile.Open(DocContext, FileAccess.Write))
using (var tempFS = tempFile.OpenRead())
{
tempFS.CopyTo(sqlFS);
scope.Complete();
}
以下是相关的查看/下载代码:
public ActionResult File(Guid? id = null)
{
if (id == null)
return RedirectToActionPermanent("Index");
return File(DocContext.Document.Find(id.Value) as File);
}
private ActionResult File(File file)
{
if (file == null)
throw new HttpException(404, "Unknown document type");
var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead });
Disposing += d => { scope.Complete(); scope.Dispose(); };
var fs = file.Open(DocContext, FileAccess.Read);
Disposing += d => fs.Dispose();
return new Misc.InlineFileStreamResult(fs, file.MimeType) { FileDownloadName = file.FileName, Inline = true };
}
和打开方法:
public partial class File
{
public SqlFileStream Open(DocumentEntities db, FileAccess access)
{
var path = db.Database.SqlQuery<string>(
@"SELECT FileData.PathName() FROM [File] WHERE DocumentID = @docID",
new SqlParameter("docID", DocumentID)).First();
var context = db.Database.SqlQuery<byte[]>(
@"SELECT Get_FILESTREAM_TRANSACTION_CONTEXT() FROM [File] WHERE DocumentID = @docID",
new SqlParameter("docID", DocumentID)).First();
return new SqlFileStream(path, context, access);
}
}
查看以前上传的文件效果很好。查看用户自己上传的文件(最近?),出现以下异常:
The transaction operation cannot be performed because there are pending requests working on this transaction.
这是怎么回事?
更新:我假设因为我是 SQL 系统管理员,所以我可以上传文件并很好地查看它们。