简体   繁体   English

将文件上传到MVC并将其流式传输到WCF

[英]Upload files to MVC and stream them to WCF

I'm currently trying to upload about 6 files to ASP.NET MVC . 我目前正在尝试将大约6个文件上传到ASP.NET MVC Once uploaded, I need to stream them to WCF , have WCF write them to the file system. 上传后,我需要将它们流式传输到WCF ,让WCF将它们写入文件系统。

I also need a way to handle the file size. 我还需要一种处理文件大小的方法。 All files are going to be images and they could be up too 100MB and I don't want the page to timeout. 所有文件都将是图像,它们可能太大了100MB,我不希望页面超时。

Could someone point me in the right direction of where to go or how to get started. 有人能指出我正确的方向或去哪里。

Here is the code I have so far: 这是我到目前为止的代码:

[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");
    }

Later I'll add something to validate if the files are actually there or not. 稍后,我将添加一些内容来验证文件是否确实存在。 Thanks in advance! 提前致谢!

Update 更新资料

After messing around a little bit, here is what I've come up with 经过一番混乱后,这就是我想出的

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");

However when the code executes, the IEnumerable<HttpPostedFileBase> files is null. 但是,在执行代码时, IEnumerable<HttpPostedFileBase> files为空。 Here is a look at my view: 这是我的看法:

    @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>
}

You can check the ContentLength . 您可以检查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));
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM