gpt4 book ai didi

c# - 如何使用 AsyncFileUpload 上传文件并将字节保存在数据库中?

转载 作者:行者123 更新时间:2023-11-30 22:36:02 26 4
gpt4 key购买 nike

我正在使用 radAsyncFileUpload 上传文件。我想将文件内容以字节形式保存在数据库中。到目前为止,这是我的代码:

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
{
if (Session["ProjectId"] != null)
{
int Projectid = Convert.ToInt32(Session["ProjectId"]);
string Description = (DetailsView1.FindControl("RadEditor1") as RadEditor).Content;
RadAsyncUpload radAsyncUpload = DetailsView1.FindControl("RadAsyncUpload1") as RadAsyncUpload;

UploadedFile file = radAsyncUpload.UploadedFiles[0];
string s = file.FileName;
string path = System.IO.Path.GetFileName(s);

radAsyncUpload.UploadedFiles[0].SaveAs(Server.MapPath("Uploads/") + path);
string Contenttype = radAsyncUpload.UploadedFiles[0].ContentType;
int fileSize = radAsyncUpload.UploadedFiles[0].ContentLength;
float length = float.Parse(fileSize.ToString());
byte[] fileData = new byte[file.InputStream.Length];
file.InputStream.Read(fileData, 0, (int)file.InputStream.Length);


ProTrakEntities objEntity = new ProTrakEntities();
ProjectFile objFile = new ProjectFile();
objFile.ProjectId = Projectid;
objFile.FileName = s;
objFile.FileType = Contenttype;
objFile.FileSize = length;
objFile.CreatedBy = "admin";
objFile.CreatedDate = DateTime.Now;
objFile.Description = Description;
objFile.FileData = fileData;
objEntity.AddToProjectFiles(objFile);
objEntity.SaveChanges();

}
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind('navigateToInserted');", true);



}
}

file.InputStream.Read(fileData, 0, (int)file.InputStream.Length); 行上传时出现错误:

Could not find file 'D:\Tiru\Projects\ProTrak\App_Data\RadUploadTemp\0ngsv0wx.fxs'.

这个错误是什么意思,我怎样才能摆脱它?

最佳答案

因为他们没有编辑权..em 发布你的代码,稍加修改看看它是否有效..我的代码

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e) 
{
if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
{
if (Session["ProjectId"] != null)
{
int Projectid = Convert.ToInt32(Session["ProjectId"]);
string Description = (DetailsView1.FindControl("RadEditor1") as RadEditor).Content;
RadAsyncUpload radAsyncUpload = DetailsView1.FindControl("RadAsyncUpload1") as RadAsyncUpload;

UploadedFile file = radAsyncUpload.UploadedFiles[0];
string s = file.FileName;
string path = System.IO.Path.GetFileName(s);


string Contenttype = radAsyncUpload.UploadedFiles[0].ContentType;
int fileSize = radAsyncUpload.UploadedFiles[0].ContentLength;
float length = float.Parse(fileSize.ToString());
byte[] fileData = new byte[file.InputStream.Length];
file.InputStream.Read(fileData, 0, (int)file.InputStream.Length);


ProTrakEntities objEntity = new ProTrakEntities();
ProjectFile objFile = new ProjectFile();
objFile.ProjectId = Projectid;
objFile.FileName = s;
objFile.FileType = Contenttype;
objFile.FileSize = length;
objFile.CreatedBy = "admin";
objFile.CreatedDate = DateTime.Now;
objFile.Description = Description;
objFile.FileData = fileData;
objEntity.AddToProjectFiles(objFile);
objEntity.SaveChanges();

}
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind('navigateToInserted');", true);

radAsyncUpload.UploadedFiles[0].SaveAs(Server.MapPath("Uploads/") + path);

}
}

关于c# - 如何使用 AsyncFileUpload 上传文件并将字节保存在数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7187029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com