我正在尝试在ASP.NET MVC中实现Gmail样式的拖放文件上传.
我一直关注这篇文章:http://robertnyman.com/html5/fileapi-upload/fileapi-upload.html并希望将上传的文件发布到MVC控制器操作.
为此,我修改了链接中的示例JavaScript脚本以指向我的控制器操作:
xhr.open("post", "/home/UploadFiles", true);
Run Code Online (Sandbox Code Playgroud)
这是我的控制器动作:
[HttpPost]
public virtual string UploadFiles(object obj)
{
var length = Request.ContentLength;
var bytes = new byte[length];
Request.InputStream.Read(bytes, 0, length);
// var bytes has byte content here. what do do next?
return "Files uploaded!";
}
Run Code Online (Sandbox Code Playgroud)
我设置了一个断点,当我上传文件时,断点被击中 - 这很好.但是如何从上传的(javascript)XMLHttpRequest对象中提取数据?我不认为它在HttpRequest中 - 它是参数吗?如果是这样,我应该期待什么类型以及如何提取字节数组并从中提取上传的文件信息?
(我使用Chrome - 我知道它在IE中不起作用)
任何建议将不胜感激!
我试图理解为什么浏览器显示<div> </ div>经文<div />有什么不同?
这是一个例子:片段#1的预期输出是三个盒子,并排:[黑色],[蓝色],[红色].片段#2仅显示[黑色]和[红色] - 为什么 [蓝色]框不在代码段#2中呈现?
<div style="float:left; width:50px; height:50px; background:black;"></div>
<div style="float:left; width:50px; height:50px; background:blue;"></div>
<div style="float:left; width:50px; height:50px; background:red;"></div>
Run Code Online (Sandbox Code Playgroud)
<div style="float:left; width:50px; height:50px; background:black;"></div>
<div style="float:left; width:50px; height:50px; background:blue;" />
<div style="float:left; width:50px; height:50px; background:red;"></div>
Run Code Online (Sandbox Code Playgroud)
编辑:我使用的是Chrome 12&html5:<!doctype html>
我有以下代码,我得到以下异常"InvalidCastException:无法将'Employee'类型的对象强制转换为'EmployeeProfile'."
private class Employee
{
public string Name { get; private set; }
public Employee()
{
this.Name = "employee";
}
public override string ToString()
{
return this.Name;
}
}
private class EmployeeProfile : Employee
{
public string Profile { get; private set; }
public EmployeeProfile() : base()
{
this.Profile = string.Format("{0}'s profile", this.Name);
}
public override string ToString()
{
return this.Profile;
}
}
public void RunTest()
{
Employee emp = new Employee();
EmployeeProfile prof = (EmployeeProfile)emp; // InvalidCastException here
System.Console.WriteLine(emp); …Run Code Online (Sandbox Code Playgroud) 我正在构建一个自定义XML文档.
以下代码执行时间超过30秒(参见注释)为什么?
var doc = new XmlDocument();
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
// hangs on next line - doc.CreateDocumentType()
var xmlDocType = doc.CreateDocumentType(
"svg"
, "-//W3C//DTD SVG 20001102//EN"
, "http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd"
, null);
doc.AppendChild(xmlDocType);
Run Code Online (Sandbox Code Playgroud)
当这花费超过30秒时,我得到以下异常:
System.Net.WebException:
"The underlying connection was closed: The connection was closed unexpectedly."
Run Code Online (Sandbox Code Playgroud)
Exception Stack Trace:
at System.Net.HttpWebRequest.GetResponse()
at System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenAndPush(Uri uri)
at System.Xml.XmlTextReaderImpl.PushExternalEntityOrSubset(String publicId, String …Run Code Online (Sandbox Code Playgroud)