当我使用ASP:File控件将文件上传到站点时,IE和Firefox中的FileName属性不同.在Firefox中,它只提供文件的名称,但IE提供了文件的完整路径.
我通过添加代码解决了这个问题:
Dim FileName As String = file.FileName
If FileName.LastIndexOf("\") > 0 Then
FileName = FileName.Substring(FileName.LastIndexOf("\") + 1)
End If
Run Code Online (Sandbox Code Playgroud)
但我不确定为什么不同的浏览器会有所不同.有谁知道这个的原因?
谢谢.
我在CSLA的分布式环境中使用MVC .NET,我可以从我的一个Web层(例如Website.MVC)引用HttpPostedFileBase ,但我不能从单独的层引用HttpPostedFileBase(我们称之为OtherLayer.Web).
关于我需要做什么才能调用HttpPostedFileBase的任何想法?我可以在两个层中使用HttpPostedFile - 我应该只使用它吗?
程序集引用基本相同 - 在Website.MVC中我有:
namespace Website.Mvc.Controllers
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web;
using System.IO;
using PetOrganizer.Mvc.Helpers;
using TrupanionPetInsurance.Web;
Run Code Online (Sandbox Code Playgroud)
而在我的另一层我有:
namespace OtherLayer.Web
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Xml;
using System.Xml.Serialization;
using Csla;
using iTextSharp.text;
using iTextSharp.text.pdf;
using TruDat.BusinessLayer;
using TruDat.BusinessLayer.Billing;
using TruDat.BusinessLayer.Data;
using TruDat.BusinessLayer.Utility; …Run Code Online (Sandbox Code Playgroud) 我有一个asp.net FileUpload控件.我可以在会话中成功上传文件到存储,但是当我想要获取其输入流(我在HttpPosterFile中存储文件)时,我收到错误
无法访问已关闭的文件
tr.PostedFile //<== HttpPostedFile;
byte[] byteArray = null;
using (var binaryReader = new BinaryReader(tr.PostedFile.InputStream))
{
byteArray = binaryReader.ReadBytes(tr.PostedFile.ContentLength);
}
Run Code Online (Sandbox Code Playgroud) 我试图使用一个FileUpload控件在aspx页面上上传多个文件.我已将控件设置为允许多个文件:
<asp:FileUpload ID="fuAttach" Multiple="Multiple" runat="server" Visible="False" />
Run Code Online (Sandbox Code Playgroud)
现在,在单击按钮时,我想获取每个文件的数据并将其保存到数据库(使用REST服务,但现在这并不重要).根据Visual Studio,我可以访问该PostedFile属性,但不能访问PostedFiles该FileUpload控件的属性.
'System.Web.UI.WebControls.FileUpload' does not contain a definition for 'PostedFiles' and no extension method 'PostedFiles' accepting a first argument of type 'System.Web.UI.WebControls.FileUpload' could be found (are you missing a using directive or an assembly reference?)
Run Code Online (Sandbox Code Playgroud)
在调试时,该PostedFiles属性是可见的并包含我的所有文件:

此外,我尝试使用Request.Files,但这只是给我FileUpload控件的id :

另外,看一下FileUpload控件,没有PostedFiles:
public class FileUpload : WebControl
{
public FileUpload();
public byte[] FileBytes { get; }
public Stream FileContent { …Run Code Online (Sandbox Code Playgroud) c# asp.net file-upload .net-framework-version httppostedfile
我有一个<asp:FileUpload>控件,其属性AllowMultiple设置为true:
<asp:fileupload id="FileUpload" runat="server" allowmultiple="true" cssclass="fileUpload btn btn-sm btn-default" onchange="preloadImages()" />
<div class="field col-md-2 col-md-offset-1 immScarpePreview MainPreviewBox">
<asp:image id="Image1" runat="server" visible="false" cssclass="img-responsive" />
<asp:button id="ImmButton" runat="server" text="Upload" onclick="ImmButton_Click" cssclass="hidden" />
</div>
Run Code Online (Sandbox Code Playgroud)
在我的JavaScript函数中,我模拟了对隐形按钮的单击:
<script>
function preloadImages() {
$('#<%=ImmButton.ClientID%>').trigger('click');
}
</script>
Run Code Online (Sandbox Code Playgroud)
在代码隐藏中,我将文件保存到临时文件夹,然后在<asp:Image>控件中显示上传的图像:
protected void ImmButton_Click(object sender, EventArgs e)
{
if (FileUpload.HasFile)
{
try
{
int cont = 0;
byte[] fileData = null;
foreach (HttpPostedFile file in FileUpload.PostedFiles)
{
if (cont == 0)
{
using (var binaryReader = new …Run Code Online (Sandbox Code Playgroud) 我们有一个将文件上传到我们网站的流程。对于用户来说,能够看到这些文件的创建时间变得很重要。我正在寻找一种从 HttpPostedFile 中提取原始创建日期的方法。如果有人对我有想法,我会非常感激(此时我有点困惑)。
我在下面有一个扩展方法,但是当我运行它时,foreach给了我InvalidCastException,它说*
无法将类型为"System.String"的对象强制转换为"System.Web.HttpPostedFile".
代码:
public static List<Attachment> GetFiles(this HttpFileCollection collection) {
if (collection.Count > 0) {
List<Attachment> items = new List<Attachment>();
foreach (HttpPostedFile _file in collection) {
if (_file.ContentLength > 0)
items.Add(new Attachment()
{
ContentType = _file.ContentType,
Name = _file.FileName.LastIndexOf('\\') > 0 ? _file.FileName.Substring(_file.FileName.LastIndexOf('\\') + 1) : _file.FileName,
Size = _file.ContentLength / 1024,
FileContent = new Binary(new BinaryReader(_file.InputStream).ReadBytes((int)_file.InputStream.Length))
});
else
continue;
}
return items;
} else
return null;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
MSDN说:
客户端使用多部分MIME格式编码文件并在内容正文中传输它们,并使用multipart/form-data的HTTP Content-Type标头.ASP.NET将编码文件从内容主体提取到HttpFileCollection的各个成员中.HttpPostedFile类的方法和属性提供对每个文件的内容和属性的访问.
我在asp.net mvc 2中上传文件时遇到问题.我的控制器功能参数是一种FormCollection类型.由于字段数太多,我不能将每个字段作为参数分开.我的表单中有2个上传文件字段.如何在控制器中上传文件?
我试过这种方式:
public ActionResult CreateAgent(FormCollection collection, HttpPostedFileBase personImage)
{
...
}
Run Code Online (Sandbox Code Playgroud)
但personImage为null.:(
或者这样:
HttpPostedFileBase img = this.HttpContext.Request.Files[collection["personImage"]];
但img就是null到.也是collection["personImage"]所选文件的名称(没有路径),我无法将其转换为HttpPostedFileBase.
请注意,所有字段必须填写一页.我不能让客户在单独的页面上传图片!