如何使用没有 runat=“server” 的表单在 ASP.NET 中处理文件上传?

发布于 2024-11-03 16:16:00 字数 1328 浏览 0 评论 0 原文

由于这个问题之外的原因,我无法在该页面上创建带有 runat="server" 属性的表单。

如何访问使用常规 上传的上传文件?

这个问题涉及到这个问题,(使用 而不是 ),但是它们仍然都 runat=server.

我希望能够实现的事情类型(服务器端,在表单发布后),包括:

  • if (MyInput.HasFile) ...
  • var fileName = MyInput. FileName;
  • var fullPathAndFile = MyInput.PostedFile.FileName;
  • var mimeType = MyInput.PostedFile.ContentType;

我确信所有这些东西都可以完成,我只是习惯了 .NET 为我处理这一切!

更新:在下面富有洞察力的评论之后,我似乎以一种奇怪的方式做事......

我最初是在寻找类似的东西:

HttpPostedFile file = Request.Files["myFile"];
//accessing the file without having the element itself being runat="server", e.g. manually through the Request.
//(I know this doesn't work without runat="server", just an example to clarify my question)

//if(MyFile.HasFile) ...
if (file != null && file.ContentLength) ...

//var fName = MyFile.FileName
var fName = Path.GetFileName(file.FileName);

但似乎即使这样也需要 runat =“服务器”

For reasons beyond this question, I cannot have a form on this page with a runat="server" attribute.

How do I go about accessing an uploaded file uploaded using a regular <input type="file"...?

This question touches on the issue, (using an <input type="file" rather than an <asp:FileUpload), however they still both runat=server.

The types of things I would like to be able to acheive (server side, after the form has been posted), include:

  • if (MyInput.HasFile) ...
  • var fileName = MyInput.FileName;
  • var fullPathAndFile = MyInput.PostedFile.FileName;
  • var mimeType = MyInput.PostedFile.ContentType;

I'm sure all of this stuff can be done, I'm just used to .NET taking care of all of this for me!

Update: after the insightful comments below, I seem to be doing things in an odd manner...

I was originally looking for something along the lines of:

HttpPostedFile file = Request.Files["myFile"];
//accessing the file without having the element itself being runat="server", e.g. manually through the Request.
//(I know this doesn't work without runat="server", just an example to clarify my question)

//if(MyFile.HasFile) ...
if (file != null && file.ContentLength) ...

//var fName = MyFile.FileName
var fName = Path.GetFileName(file.FileName);

But it seems that even that requires runat="server"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若能看破又如何 2024-11-10 16:16:01

创建自定义 HtmlForm 并按如下方式打开和关闭它:

自定义 HtmlForm:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;


namespace CustomForm
{
    public class GhostForm : System.Web.UI.HtmlControls.HtmlForm
    {
        protected bool _render;

        public bool RenderFormTag
        {
            get { return _render; }
            set { _render = value; }
        }

        public GhostForm()
        {
            //By default, show the form tag
            _render = true;
        }

        protected override void RenderBeginTag(HtmlTextWriter writer)
        {
            //Only render the tag when _render is set to true
            if (_render)
                base.RenderBeginTag(writer);
        }     

        protected override void RenderEndTag(HtmlTextWriter writer)
        {
            //Only render the tag when _render is set to true
            if (_render)
                base.RenderEndTag(writer);
        }
    }
}

用法:

public partial class MyClass : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          GhostForm mainForm = new GhostForm();
          mainForm.RenderFormTag = false;
            .....    
      }
          // Upload your file, etc.
      .....
  }

Create a custom HtmlForm and toggle it on and off as follows:

Custom HtmlForm:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;


namespace CustomForm
{
    public class GhostForm : System.Web.UI.HtmlControls.HtmlForm
    {
        protected bool _render;

        public bool RenderFormTag
        {
            get { return _render; }
            set { _render = value; }
        }

        public GhostForm()
        {
            //By default, show the form tag
            _render = true;
        }

        protected override void RenderBeginTag(HtmlTextWriter writer)
        {
            //Only render the tag when _render is set to true
            if (_render)
                base.RenderBeginTag(writer);
        }     

        protected override void RenderEndTag(HtmlTextWriter writer)
        {
            //Only render the tag when _render is set to true
            if (_render)
                base.RenderEndTag(writer);
        }
    }
}

Usage:

public partial class MyClass : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          GhostForm mainForm = new GhostForm();
          mainForm.RenderFormTag = false;
            .....    
      }
          // Upload your file, etc.
      .....
  }
天邊彩虹 2024-11-10 16:16:01

这个问题似乎有点困惑。

首先,页面上有另一个表单是什么意思? ASP.NET 页面应该具有完全相同的一个 形式(带或不带runat="server")。

这种措辞让我认为你还有另一个应该首先解决的问题。在 ASP.NET 页面上出现多个表单具有正当理由的情况极为罕见。

但如果这是您真正需要的,请删除问题中的 ASP.NET 标记并将其替换为 HTML,因为这与 ASP.NET 无关。

This question seems a little confused.

First off, what do you mean about having another form on a page? ASP.NET pages should have exactly one form (with or without runat="server").

This wording makes me think you have another issue that should be addressed first. It's extremely unusual to have a valid reason for more than one form on an ASP.NET page.

But if this is what you really need, then remove the ASP.NET tags to your question and replace them with HTML as this would have nothing to do with ASP.NET.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文