C# 如何使用实体框架将文件上载到一个列中到服务器?
如何在一行中上载/保存文件 我编写的代码将两行保留为null,然后将其他文件插入新行,以代替第一行中的null值。以下是插入到db中的内容的图像: 以下是我希望插入的内容: 在检查上载输入是否包含文件后,如何使代码将文件插入一行C# 如何使用实体框架将文件上载到一个列中到服务器?,c#,sql-server,entity-framework,row,C#,Sql Server,Entity Framework,Row,如何在一行中上载/保存文件 我编写的代码将两行保留为null,然后将其他文件插入新行,以代替第一行中的null值。以下是插入到db中的内容的图像: 以下是我希望插入的内容: 在检查上载输入是否包含文件后,如何使代码将文件插入一行 if (resume.HasFile == true && resume.PostedFile.ContentLength > 0) { string fileNameWithoutExtension = Path.GetFileNam
if (resume.HasFile == true && resume.PostedFile.ContentLength > 0)
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(resume.FileName);
string extension = Path.GetExtension(resume.FileName);
string resumeName = "resume" + extension;
BinaryReader Binary_Reader = new BinaryReader(resume.PostedFile.InputStream);
byte[] File_Buffer = Binary_Reader.ReadBytes(resume.PostedFile.ContentLength);
Binary_Reader = new BinaryReader(resume.PostedFile.InputStream);
File_Buffer = Binary_Reader.ReadBytes(resume.PostedFile.ContentLength);
myServiceCP.LicenseApplicationAttachments.Add(new LicenseApplicationAttachment
{
resumeName = resumeName,
CompanyName = control.CompanyName,
userid = Guid.NewGuid(),
resumeContentType = resume.PostedFile.ContentType,
resumeExtension = Path.GetExtension(resume.PostedFile.FileName),
resumeSize = resume.PostedFile.ContentLength,
resumeContent = File_Buffer
});
}
if (cv.HasFile == true)
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(cv.FileName);
string extension = Path.GetExtension(cv.FileName);
string cvFileName = "cv" + extension;
BinaryReader Binary_Reader = new BinaryReader(cv.PostedFile.InputStream);
byte[] File_Buffer = Binary_Reader.ReadBytes(cv.PostedFile.ContentLength);
Binary_Reader = new BinaryReader(cv.PostedFile.InputStream);
File_Buffer = Binary_Reader.ReadBytes(cv.PostedFile.ContentLength);
myServiceCP.LicenseApplicationAttachments.Add(new LicenseApplicationAttachment
{
cvFileName = cvFileName,
CompanyName = control.CompanyName,
cvContentType = CapitalStructureShareHoldingFile.PostedFile.ContentType,
cvExtension = Path.GetExtension(CapitalStructureShareHoldingFile.PostedFile.FileName),
cvFileSize = CapitalStructureShareHoldingFile.PostedFile.ContentLength,
cvFileContent = File_Buffer
});
}
myServiceCP.SaveChanges();
}
你应该有一个变量并添加你必须开始的信息, 然后在if语句中向变量添加所需的信息 然后将变量添加到数据库中 像这样:
if (resume.HasFile == true && resume.PostedFile.ContentLength > 0)
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(resume.FileName);
string extension = Path.GetExtension(resume.FileName);
string resumeName = "resume" + extension;
BinaryReader Binary_Reader = new BinaryReader(resume.PostedFile.InputStream);
byte[] File_Buffer = Binary_Reader.ReadBytes(resume.PostedFile.ContentLength);
Binary_Reader = new BinaryReader(resume.PostedFile.InputStream);
File_Buffer = Binary_Reader.ReadBytes(resume.PostedFile.ContentLength);
var applicationToAdd = new LicenseApplicationAttachment
{
resumeName = resumeName,
CompanyName = control.CompanyName,
userid = Guid.NewGuid(),
resumeContentType = resume.PostedFile.ContentType,
resumeExtension = Path.GetExtension(resume.PostedFile.FileName),
resumeSize = resume.PostedFile.ContentLength,
resumeContent = File_Buffer
};
if (cv.HasFile == true)
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(cv.FileName);
string extension = Path.GetExtension(cv.FileName);
string cvFileName = "cv" + extension;
BinaryReader Binary_Reader = new BinaryReader(cv.PostedFile.InputStream);
byte[] File_Buffer = Binary_Reader.ReadBytes(cv.PostedFile.ContentLength);
Binary_Reader = new BinaryReader(cv.PostedFile.InputStream);
File_Buffer = Binary_Reader.ReadBytes(cv.PostedFile.ContentLength);
applicationToAdd.cvFileName = cvFileName;
applicationToAdd.CompanyName = control.CompanyName;
applicationToAdd.cvContentType = CapitalStructureShareHoldingFile.PostedFile.ContentType;
applicationToAdd.cvExtension = Path.GetExtension(CapitalStructureShareHoldingFile.PostedFile.FileName);
applicationToAdd.cvFileSize = CapitalStructureShareHoldingFile.PostedFile.ContentLength;
applicationToAdd.cvFileContent = File_Buffer;
}
myServiceCP.LicenseApplicationAttachments.Add(applicationToAdd);
myServiceCP.SaveChanges();
}
您可以在表中使用Insert。表名和字段是什么?检查数据库模型。@jdweng,我不把它包括在代码中,因为它已经插入了,但我想把第一个if语句和第二个insert语句合并到一个列中。现在,它在一行中插入第一个if,并将其他列保留为null,然后转到第二个语句,插入第二行,并将其他字段(列)保留为null。为什么是一列?然后在提取时需要拆分两个对象(文件和文件名)。如果您只需要一列,那么您将在二进制文件上放置一个ASCII头。所以您需要使用byte[]头编码.ASCII.GetBytes(“string”+“@”);我使用“@”字符作为分隔符,然后存储文件和头。使用二进制读取器.ReadBytes(标头)@jdweng,我已经编辑了这篇文章,其中包含了如何插入内容以及我希望如何插入内容的链接。您将进入IF语句并创建两行。如果(resume.HasFile==true&&resume.PostedFile.ContentLength>0)当HasFile==true和ContentLength>0时,将执行这两个if。但它与myServiceCP.LicenseApplicationAttachments.Add(applicationAdd)一起工作代码>位于大括号顶部