上传文件保存字节流,下载
2017-12-19 16:22
197 查看
public ActionResult ExcelUpLoad(HttpPostedFileBase file, string type)
{
string filePath = string.Empty;
var contentType = "";
if (file != null)
{
try
{
Stream stream = null;
stream = file.InputStream;
var fileBinary = new byte[stream.Length];
stream.Read(fileBinary, 0, fileBinary.Length);
contentType = file.ContentType;
TopicexcelFileName = file.FileName;
string fileName = file.FileName;
fileName = fileName.Substring(fileName.LastIndexOf("."));
string StrType = fileName.Split('.')[1];
ViewBag.fileType = StrType;
ViewBag.fileName = file.FileName;
//string fileName_des = Guid.NewGuid().ToString();
//fileName_des = fileName_des + "." + StrType;
string uploadFolder = "/Excel/" + TopicexcelFileName;
// file.SaveAs(Topicexcelfilepath + "/" + TopicexcelFileName);
filePath = uploadFolder;
ViewBag.filePath = filePath;
ViewBag.type = type;
int AttachmentTypeId = 0;
string AttachmentTypeName = "";
if (type == "1")
{
AttachmentTypeId = 1;
AttachmentTypeName = "";
}
if (type == "2")
{
AttachmentTypeId = 2;
AttachmentTypeName = "";
}
var downloadCopy = new Download
{
DownloadGuid = Guid.NewGuid(),
UseDownloadUrl = false,
DownloadBinary = fileBinary,
ContentType = contentType,
Filename = TopicexcelFileName.Substring(0, TopicexcelFileName.LastIndexOf(".")),
Extension = TopicexcelFileName.Substring(TopicexcelFileName.LastIndexOf(".")),
EntityName = "QuoteRecord",
AttachmentTypeId = AttachmentTypeId,
AttachmentTypeName = AttachmentTypeName,
IsNew = true,
DisplayOrder = 0,
CreatedOnUtc = System.DateTime.UtcNow,
UpdatedDateTime = System.DateTime.UtcNow,
IsDeleted = false
};
Insert(downloadCopy);
ViewBag.DownloadId = downloadCopy.Id;
}
catch (Exception ex)
{
ViewBag.fileName = "";
ViewBag.filePath = ex.Message;
}
}
else
{
ViewBag.fileName = "";
ViewBag.filePath = "";
}
return View();
}
{
string filePath = string.Empty;
var contentType = "";
if (file != null)
{
try
{
Stream stream = null;
stream = file.InputStream;
var fileBinary = new byte[stream.Length];
stream.Read(fileBinary, 0, fileBinary.Length);
contentType = file.ContentType;
TopicexcelFileName = file.FileName;
string fileName = file.FileName;
fileName = fileName.Substring(fileName.LastIndexOf("."));
string StrType = fileName.Split('.')[1];
ViewBag.fileType = StrType;
ViewBag.fileName = file.FileName;
//string fileName_des = Guid.NewGuid().ToString();
//fileName_des = fileName_des + "." + StrType;
string uploadFolder = "/Excel/" + TopicexcelFileName;
// file.SaveAs(Topicexcelfilepath + "/" + TopicexcelFileName);
filePath = uploadFolder;
ViewBag.filePath = filePath;
ViewBag.type = type;
int AttachmentTypeId = 0;
string AttachmentTypeName = "";
if (type == "1")
{
AttachmentTypeId = 1;
AttachmentTypeName = "";
}
if (type == "2")
{
AttachmentTypeId = 2;
AttachmentTypeName = "";
}
var downloadCopy = new Download
{
DownloadGuid = Guid.NewGuid(),
UseDownloadUrl = false,
DownloadBinary = fileBinary,
ContentType = contentType,
Filename = TopicexcelFileName.Substring(0, TopicexcelFileName.LastIndexOf(".")),
Extension = TopicexcelFileName.Substring(TopicexcelFileName.LastIndexOf(".")),
EntityName = "QuoteRecord",
AttachmentTypeId = AttachmentTypeId,
AttachmentTypeName = AttachmentTypeName,
IsNew = true,
DisplayOrder = 0,
CreatedOnUtc = System.DateTime.UtcNow,
UpdatedDateTime = System.DateTime.UtcNow,
IsDeleted = false
};
Insert(downloadCopy);
ViewBag.DownloadId = downloadCopy.Id;
}
catch (Exception ex)
{
ViewBag.fileName = "";
ViewBag.filePath = ex.Message;
}
}
else
{
ViewBag.fileName = "";
ViewBag.filePath = "";
}
return View();
}
相关文章推荐
- C# FileUpload上传视频、图片、文档,分别保存在不同的文件夹中以及下载文件的方法
- Silverlight文件上传下载(下载保存)
- 上传文件 IE不兼容的问题[提示下载保存]
- Extjs 上传文件 IE不兼容的问题[提示下载保存]
- WPF用流的方式上传/显示/下载图片文件(保存在数据库)
- 文件上传下载:通过java方法把附件保存到clob字段中
- Extjs 上传文件 IE不兼容的问题[提示下载保存]
- 上传下载保存到数据库的image字段中的文件
- 将文件上传、下载(以二进制流保存到数据库)
- C#将文件上传、下载(以二进制流保存到数据库)
- WPF用流的方式上传/显示/下载图片文件(保存在数据库)
- ASP.NET中文件上传下载方法集合(上传限制,保存方式-二进制或文件形式,上传Internet上的资源 )
- ASP.NET中文件上传下载方法集合(上传限制,保存方式-二进制或文件形式,上传Internet上的资源 )
- ASP.NET MVC文件上传下载Demo(文件保存在数据库)
- springMvc 中 Excel批量数据上传,利用POI解析数据保存入数据库 及模板文件下载
- 用FileReference实现下载,上传的文件会保存在哪个路径下?
- ASP.NET中文件上传下载方法集合(上传限制,保存方式-二进制或文件形式,上传Internet上的资源 )
- 将文件上传、下载(以二进制流保存到数据库)
- Selenium2自动化测试:让Firefox无提示地上传、下载保存文件
- java http下载文件/上传文件保存