如何解决在使用C#的ASP.NET文件上载过程中未找到上传模板的问题? [英] How to resolve issue of a template uploaded not found as file resource error during file upload in ASP.NET with C#?

查看:90
本文介绍了如何解决在使用C#的ASP.NET文件上载过程中未找到上传模板的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现上传的PDF模板在文件上传过程中未找到文件资源错误。我已托管该应用程序。如果我在托管应用程序中上传文件没有问题。否则我收到错误E:FileUpload.pdf未找到资源或文件异常。我实际上是从使用文件上传上传的PDF中提取数据。



I am having issue of a PDF template uploaded not found as file resource error during file upload. I have hosted the application. If I upload the files with in the hosted application no issues. Else I have getting error "E:FileUpload.pdf" not found as resource or file exception. I am actually extracting data from the PDF uploaded using file upload.

string Orginalfilename = Path.GetFileName(fupload.PostedFile.FileName);

               FilePath = fupload.PostedFile.FileName;
               //FilePath += Orginalfilename;
               PdfReader pdfReader = new PdfReader(FilePath);
               AcroFields pdfFormFields = pdfReader.AcroFields;





我的尝试:





What I have tried:

<pre> string Orginalfilename = Path.GetFileName(fupload.PostedFile.FileName);
               
                FilePath = fupload.PostedFile.FileName;
                //FilePath += Orginalfilename;
                PdfReader pdfReader = new PdfReader(FilePath);
                AcroFields pdfFormFields = pdfReader.AcroFields;



web.config设置如下


web.config set as below

<httpRuntime maxRequestLength="20480" executionTimeout="3600" enable="true"/>




<security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="1073741824" />
    </requestFiltering>

  </security>

推荐答案

//Having all CRUD operations related to file.

<pre>public class FilesController : Controller
    {

        FileDownload obj;
        public FilesController()
        {
            obj = new FileDownload();
        }
        // GET: Files
        public ActionResult Index()
        {
            var filesCollection = obj.GetFiles();
            return View(filesCollection);
        } 
        //Download particular using it's ID 
        public FileResult Download(string FileID)
        {
              
            string CurrentFileName = obj.GetFileNameById(FileID);

            string contentType = string.Empty;

            contentType = extentionDetector(CurrentFileName);
            
            return File(CurrentFileName, contentType, CurrentFileName);
        }

        // Delete file using file name
        public ActionResult DeleteFile(string fileName)
        {
             
            var fullPath = Server.MapPath("~/MyFiles/" + fileName);

            if (System.IO.File.Exists(fullPath))
            {
                System.IO.File.Delete(fullPath);
                ViewBag.deleteSuccess = "true";
            }
            return Redirect("index");
        }
        //get the format of the file using this Detector 
        private string extentionDetector(string CurrentFileName) {

            string contentType = string.Empty;
            if (CurrentFileName.Contains(".pdf"))
            {
                contentType = "application/pdf";
            }

            else if (CurrentFileName.Contains(".docx"))
            {
                contentType = "application/docx";
            }
            else if (CurrentFileName.Contains(".txt"))
            {
                contentType = "application/txt";
            }
            else if (CurrentFileName.Contains(".png"))
            {
                contentType = "application/png";
            }
            return contentType;
        }	
        //upload file method, after submitting rquest	
        [HttpPost]
        public ActionResult FileUpload(IEnumerable<HttpPostedFileBase> files)
        {
            foreach (HttpPostedFileBase file in files)
            {
                if (file != null && file.ContentLength > 0)
                    try
                    {
                        string path = Path.Combine(Server.MapPath("~/MyFiles"),
                                                   Path.GetFileName(file.FileName));
                        file.SaveAs(path);
                    }
                    catch (Exception ex)
                    {
                        ViewBag.Message = "ERROR:" + ex.Message.ToString();
                    }
                else
                {
                    ViewBag.Message = "You have not specified a file.";
                }
            }
            return Redirect("index");
        }

    }





用于问题参考。您有服务器路径问题,我的代码中已解决此问题。希望如此,它对您有所帮助。



Use it for your problem reference. You have server path issues, which is resolved in my code. Hope so, it's helpful for you.


这篇关于如何解决在使用C#的ASP.NET文件上载过程中未找到上传模板的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆