我上传文件时遇到问题, [英] I am having trouble with uploading a file,
本文介绍了我上传文件时遇到问题,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I am using Devexpress, When I choose a valid file to upload it says "Object reference not set to an instance of an object." If I put the filepath and into into the database manually and add the file to folder where web.config is set to I can see the file and download it. this is the part of code that breaks when trying
protected void DocumentUpload_OnFileUploadComplete(object sender, FileUploadCompleteEventArgs e)
{
if (!e.UploadedFile.IsValid) return;
GridViewDetailRowTemplateContainer container = ((Control)sender).NamingContainer as GridViewDetailRowTemplateContainer;
Int32 index = container.VisibleIndex;
var orderId = Convert.ToInt32(ViewOrdersGridView.GetRowValues(index, ViewOrdersGridView.KeyFieldName));
var fileNumber = ViewOrdersGridView.GetRowValues(index, ElsaConstants.FileNumber).ToString();
var userId = Convert.ToInt32(Session[ElsaConstants.UserId]);
string directory = ConfigurationManager.AppSettings[ElsaConstants.UploadDirectory];
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
string fileType = Path.GetExtension(e.UploadedFile.FileName);
string fileName = e.UploadedFile.FileName.Replace(fileType, string.Format("_{0}{1}", fileNumber, fileType));
string resultFilePath = directory + fileName;
e.UploadedFile.SaveAs(resultFilePath, true);//MapPath
using (var context = new ELSAEntities())
{
var document = new Document
{
OrderId = orderId,
DocumentPath = fileName,
DateCreated = DateTime.Now,
CreatedBy = userId
};
context.Document.Add(document);
context.SaveChanges();
}
}<pre><pre><pre lang="c#">
推荐答案
尝试设置网格的键值。
这篇关于我上传文件时遇到问题,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文