【问题标题】:ASP.NET MVC5 copy HTTP Post File from temporary memory to serverASP.NET MVC5 将 HTTP Post 文件从临时内存复制到服务器
【发布时间】:2015-05-08 08:46:42
【问题描述】:

作为一个相对较新的 asp.net,对它的所有特性和功能都不是很了解。我想从更有经验的用户那里得到一些建议。

我正在尝试找到通过服务器端脚本(最有可能是 Java)将文件从 1 台或多台远程计算机自动上传到文件服务器的最佳方法。该服务器正在运行 windows server 2012 并托管一个 ASP.NET MVC 网站。不幸的是,由于我们的 IT 基础设施,FTP 不是一个选项。

我一直在研究使用 HTTP POST/PUT & GET 方法在远程计算机和使用网站作为网关的文件存储服务器之间进行文件传输。 我已经成功实现了一个测试项目,当用户选择文件并单击提交按钮时,从 ASP.NET 网页网页将文件上传到服务器。我现在想将其扩展到当网页处理 HTTP Post 请求时,它将它从临时内存复制到服务器。

非常感谢任何 cmets 帮助。

问候,

提姆

【问题讨论】:

  • 你的意思是HttpPostedFile.SaveAs(string)?请显示更多代码和更少文本。 ;)
  • 是的,我正在使用 file.SaveAs,我将发布代码。

标签: c# asp.net asp.net-mvc-5


【解决方案1】:

这是 Model.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using amrcadpWebDev.Controllers;

namespace amrcadpWebDev.Models
{
    public class FileModels
    {
        public HttpPostedFileBase amrcadpFile { get; set; }
    }
}

这是 FileController.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace amrcadpWebDev.Controllers
{
    /// <summary>
    /// File Controller
    /// </summary>
    public class FileController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        #region Action

        [HttpPost]
        public virtual ActionResult FileUpload(HttpPostedFileBase File)
        {
            bool isUploaded = false;
            string message = "File upload failed";

            if (ModelState.IsValid)
            {
                if (File != null && File.ContentLength != 0 )
                {
                    string pathForSaving = "\\\\*****\\FileStore\\resultsFiles\\";
                    string fname = Path.Combine(pathForSaving, File.FileName);
                    try
                    {
                        File.SaveAs(Path.Combine(pathForSaving, File.FileName));
                        isUploaded = true;
                        message = "File uploaded successfully!";
                    }
                    catch (Exception ex)
                    {
                        message = string.Format("File upload failed!!: {0} {1}", ex.Message, fname);
                    }
                }

                return Json(new { isUploaded = isUploaded, message = message }, "text/html");
            }

            return View();
        }

        #endregion

    }   
}

这是视图/Index.cs

@model amrcadpWebDev.Models.FileModels

<h2>Basic File Upload</h2>
@using (Html.BeginForm ("FileUpload",
                        "File",
                        FormMethod.Post,
                        new { enctype = "multipart/form-data" }))
{
    <label for="file">Upload File:</label>
    <input type="file" name="file" id="file" /><br><br>
    <input type="submit" value="Upload File" />
    <br><br>
    <label for="file">cRIO File String:</label>
    <input type="submit" value="Get File" />

    <p>
        @Html.ActionLink("get cRIO", "GetFile")
    </p>

    @ViewBag.Message
}
@Html.DisplayNameFor(model => model.amrcadpFile)

这适用于用户发起的文件选择和提交。我将如何调整它以管理带有文件数据的 http 帖子,然后我会将其从临时内存保存到服务器。

我希望我的解释很清楚任何问题请评论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 2011-02-04
    • 2011-12-03
    相关资源
    最近更新 更多