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);
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"
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.
.....
}
首先,页面上有另一个表单是什么意思? 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.
发布评论
评论(2)
创建自定义 HtmlForm 并按如下方式打开和关闭它:
自定义 HtmlForm:
用法:
Create a custom HtmlForm and toggle it on and off as follows:
Custom HtmlForm:
Usage:
这个问题似乎有点困惑。
首先,页面上有另一个表单是什么意思? 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.