ASP.NET-上传/下载文件时出错

时间:2018-10-03 15:55:02

标签: asp.net

仅提供一些初始上下文,我有一个ASP.NET MVC应用程序,用户可以在其中创建一个帐户,登录并填写一系列可以随时保存的表格。这些表格由不同的选项卡分隔。在选项卡之一中,用户可以上传和下载JPEG和PDF文件。此功能已实施了几周,起初它运行良好。文件上传没有问题,我可以下载它们,打开它们并查看其内容而不会出现问题。但是最近,每次我上载PDF或JPEG文件时,下载并打开它时,总是会出现错误。对于JPEG文件,我总是收到一条错误消息,指出不支持文件类型。对于PDF,我总是会收到一条错误消息,指出文件无法加载。请记住,自从我第一次实现并注意到它可以工作以来,我从未接触过此代码。

这是有问题的代码。上面的功能负责上传PDF和JPEG文件。另一个负责下载所述文件:

[HttpPost]
        public ActionResult Upload(Documento documento, HttpPostedFileBase file)
        {
            int userId = (int)Session["userID"];

            using (LoginDataBaseEntities1 dbModel = new LoginDataBaseEntities1())
            {
                try
                {
                    if (file != null && file.ContentLength > 0)
                    {
                        if (this.IsJpeg(file) || this.IsPdf(file))
                        {
                            var fileName = Path.GetFileName(file.FileName);

                            MemoryStream target = new MemoryStream();
                            file.InputStream.CopyTo(target);
                            byte[] data = target.ToArray();

                            Documento doc = new Documento();

                            doc.Descricao = documento.Descricao;
                            doc.Nome = fileName;
                            doc.Ficheiro = data;

                            dbModel.Documentoes.Add(doc);

                            UserDocumento userDocumento = new UserDocumento();

                            userDocumento.UserId = userId;
                            userDocumento.DocumentoId = doc.ID;

                            dbModel.UserDocumentoes.Add(userDocumento);

                            dbModel.SaveChanges();
                        }
                        else
                        {
                            Session["ErrorDoc"] = 1;

                            Session["SelectedTab"] = 4;

                            return RedirectToAction("Index", "Home");
                        }
                    }

                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                {
                    Exception raise = dbEx;
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage);
                            raise = new InvalidOperationException(message, raise);
                        }
                    }
                    throw raise;
                }
            }
            ModelState.Clear();

            Session["ErrorDoc"] = 0;

            Session["SelectedTab"] = 4;

            return RedirectToAction("Index", "Home");
        }

    public ActionResult DescarregarDocumento(int id)
            {
                using (LoginDataBaseEntities1 dbModel = new LoginDataBaseEntities1())
                {
                    try
                    {
                        Documento doc = dbModel.Documentoes.Where(dp => dp.ID == id).FirstOrDefault();

                        Response.Clear();
                        Response.Buffer = true;
                        Response.Charset = "";
                        Response.Cache.SetCacheability(HttpCacheability.NoCache);
                        Response.ContentType = Path.GetExtension(doc.Nome);
                        Response.AppendHeader("Content-Disposition", "attachment; filename=" + doc.Nome);
                        Response.BinaryWrite(doc.Ficheiro);
                        Response.Flush();
                        Response.End();
                    }
                    catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                    {
                        Exception raise = dbEx;
                        foreach (var validationErrors in dbEx.EntityValidationErrors)
                        {
                            foreach (var validationError in validationErrors.ValidationErrors)
                            {
                                string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage);
                                raise = new InvalidOperationException(message, raise);
                            }
                        }
                        throw raise;
                    }
                }

                Session["SelectedTab"] = 4;

                return RedirectToAction("Index", "Home");
            }

在调试时,我注意到HttpPostedFileBase的InputStream属性始终返回空,因此为什么我不断收到这些错误。对于InputStream的ReadTimeout和WriteTimeout属性,我还遇到了以下异常:

+       ReadTimeout 'file.InputStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'    int {System.InvalidOperationException}

+       WriteTimeout    'file.InputStream.WriteTimeout' threw an exception of type 'System.InvalidOperationException'   int {System.InvalidOperationException}

0 个答案:

没有答案
相关问题