C# 将文件上载到MVC并流式传输到WCF
我目前正在尝试将大约6个文件上载到C# 将文件上载到MVC并流式传输到WCF,c#,asp.net-mvc,wcf,asp.net-mvc-4,C#,Asp.net Mvc,Wcf,Asp.net Mvc 4,我目前正在尝试将大约6个文件上载到ASP.NET MVC。上传后,我需要将它们流式传输到WCF,让WCF将它们写入文件系统 我还需要一种处理文件大小的方法。所有的文件都将是图像,它们可能会达到100MB,我不希望页面超时 有人能告诉我去哪里或如何开始的正确方向吗 以下是我目前掌握的代码: [HttpPost] public ActionResult Index(FileUpload file) { foreach (string upload in Request
ASP.NET MVC
。上传后,我需要将它们流式传输到WCF
,让WCF
将它们写入文件系统
我还需要一种处理文件大小的方法。所有的文件都将是图像,它们可能会达到100MB,我不希望页面超时
有人能告诉我去哪里或如何开始的正确方向吗
以下是我目前掌握的代码:
[HttpPost]
public ActionResult Index(FileUpload file)
{
foreach (string upload in Request.Files)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
string filename = Path.GetFileName(Request.Files[upload].FileName);
Request.Files[upload].SaveAs(Path.Combine(path, filename));
}
TempData["Status"] = "Your files were successfully upload.";
return RedirectToAction("Index", "Employees");
}
稍后,我将添加一些内容来验证文件是否确实存在。提前谢谢
更新
在胡闹了一点之后,我想到了
foreach (var file in files)
{
if (file.ContentLength > 0)
{
FileUploadStream uploadedFile = null;
uploadedFile.FileName = file.FileName;
uploadedFile.StreamData = file.InputStream;
uploadedFile.FileSize = file.ContentLength;
BinaryReader b = new BinaryReader(file.InputStream);
byte[] binData = b.ReadBytes(file.ContentLength);
using (Proxy<IFileUpload> Proxy = new Proxy<IFileUpload>())
{
uploadedFile = Proxy.Channel.SaveFiles(uploadedFile.FileName, binData);
Proxy.Close();
}
}
else
{
TempData["Error"] = "Didn't work :(";
return RedirectToAction("Index", "Employees");
}
}
TempData["Status"] = "Holy crap, it worked :)";
return RedirectToAction("Index", "Employees");
foreach(文件中的var文件)
{
如果(file.ContentLength>0)
{
FileUploadStream uploadedFile=null;
uploadedFile.FileName=文件名;
uploadedFile.StreamData=file.InputStream;
uploadedFile.FileSize=file.ContentLength;
BinaryReader b=新的BinaryReader(file.InputStream);
byte[]binData=b.ReadBytes(file.ContentLength);
使用(代理=新代理())
{
uploadedFile=Proxy.Channel.SaveFiles(uploadedFile.FileName,binData);
Proxy.Close();
}
}
其他的
{
TempData[“Error”]=“无效:(”;
返回操作(“索引”、“员工”);
}
}
TempData[“状态”]=“天哪,它工作了:)”;
返回操作(“索引”、“员工”);
但是,当代码执行时,IEnumerable files
为空。以下是我的观点:
@model Common.Contracts.DataContracts.FileUploadStream
@{
ViewBag.Title = "Upload Tool";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>IFileUpload</legend>
PHOTO <input type="file" name="signUpload" />
<br />
<input type="submit" name="Submit" id="Submit" value="Upload Files" />
</fieldset>
}
@model Common.Contracts.DataContracts.FileUploadStream
@{
ViewBag.Title=“上传工具”;
}
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
IFileUpload
照片
}
您可以检查ContentLength
for (int i = 0; i < this.Request.Files.Count; i++)
{
var file = this.Request.Files[i];
//
// ContentLength comes in bytes, you must convert it to MB and then
// compare with 100 MB
//
if(file.ContentLength / 1024 / 1024 > 100)
{
TempData["Status"] += file.FileName + " has It size greater than 100MB";
}
else
{
string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
string filename = Path.GetFileName(file.FileName);
file.SaveAs(Path.Combine(path, filename));
}
}
for(int i=0;i100)
{
TempData[“Status”]+=file.FileName+“其大小大于100MB”;
}
其他的
{
字符串路径=AppDomain.CurrentDomain.BaseDirectory+“uploads/”;
字符串文件名=Path.GetFileName(file.filename);
file.SaveAs(Path.Combine(Path,filename));
}
}