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

来源:互联网 发布:java的后缀名 编辑:程序博客网 时间:2024/05/22 14:19
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();
        }
原创粉丝点击