您的位置:首页 > 编程语言 > C#

上传文件保存字节流,下载

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();

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# 上传
相关文章推荐