日期:2014-05-18  浏览次数:20424 次

MVC上传文件
现在的问题是:我希望能将文件上传到一台IP为10.101.10.1的服务器上,怎么写后台代码?

  /// <summary>
  /// 上传文件
  /// </summary>
  /// <param name="path">文件上传到的路径</param>
  private void uploadFile(string path, out List<ElectronicAssistantModel> ElectronicAssistantList)
  {
  ElectronicAssistantList = new List<ElectronicAssistantModel>();
  foreach (string upload in Request.Files)
  {
  if (!Request.Files[upload].HasFile()) continue;

  if (!Directory.Exists(path))
  {
  Directory.CreateDirectory(path);
  }
  string filename = Path.GetFileName(Request.Files[upload].FileName);

  Request.Files[upload].SaveAs(Path.Combine(path, filename));
  ElectronicAssistantList.Add(new ElectronicAssistantModel
  {
  DocumentName = filename,
  Url = path
  });
  }
  }

这个上传到本服务器没有问题?但是这个 要上传到网络路径,怎么搞
网上有说在web.config里配置<identity impersonate="true" userName="test" password= "123456"/>
我没成功,期待高手帮忙!!!!

------解决方案--------------------
如果你是跨域上传 要嘛写webservice 要嘛在10.101.10.1弄个上传页面 然后用内框架链接到当前站点 
 第二种我没用过 不知道可行不可行
------解决方案--------------------
C# code
namespace AppGet {
    public class PushCommand : CommandBase {
        protected override void Execute() {
            string uri = string.Format("http://{0}/App/Push/{1}", ConfigurationManager.AppSettings["AppCenter"], Args["-file"]);
            Console.WriteLine(uri);
            HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;
            request.Method = "POST";

            using (Stream stream = request.GetRequestStream()) {
                var buffer = new byte[4096];
                using (FileStream fs = new FileStream(Args["-file"], FileMode.Open)) {
                    while (true) {
                        int r = fs.Read(buffer, 0, 4096);
                        if (r <= 0) { break; }
                        stream.Write(buffer, 0, r);
                    }
                }
            }

            request.GetResponse();
        }

        private const string DefaultContentType = "application/octet";
    }
}

------解决方案--------------------
前面copy的是csharp client upload的代码
下面是mvc中接受文件的代码
C# code
[AcceptVerbs(HttpVerbs.Post)]
        public void Push(string id) {
            string appName = id.Split('.')[0];
            string targetFolder = Path.Combine(Server.MapPath("/Apps"), appName);
            if (!Directory.Exists(targetFolder)) {
                Directory.CreateDirectory(targetFolder);
            }

            var buffer = new byte[4096];

            using (FileStream fs = new FileStream(Path.Combine(targetFolder, id), FileMode.Create)) {
                while (true) {
                    int r = Request.InputStream.Read(buffer, 0, 4096);
                    if (r <= 0) { break; }
                    fs.Write(buffer, 0, r);
                }
            }
        }

------解决方案--------------------
mvc中上传文件
C# code
[AcceptVerbs(HttpVerbs.Post)]
        public ContentResult Upload(HttpPostedFileBase FileData, string folder) {
            string result = "";
            if (null != FileData) {
                try {