每次从客户端PC获取图像上传到服务器 [英] Every time take images from client PC to upload to server

查看:105
本文介绍了每次从客户端PC获取图像上传到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已从textbox ex读取本地路径。 D:\ images并使用server.mappath上传到服务器,它在localhost上工作正常



但是代码发布到服务器的源路径D:\ images考虑从服务器路径而不是从客户机。



我想从客户端机器读取这条路径,将所有图像从文件夹上传到服务器。



我的尝试:



I have read local path from textbox ex. D:\images and uploaded to server by using server.mappath its working fine on localhost

but while code published to server the source path D:\images considered from server path not from client machine .

and I want to read this path from client machine to upload all images from folder to server.

What I have tried:

string folderPath = System.Configuration.ConfigurationManager.AppSettings["UploadPath"].ToString();

  DirectoryInfo d = new DirectoryInfo(@MD.Path);//here I get D:\images 
   FileInfo[] Files = ((d.GetFiles("*.jpg", SearchOption.AllDirectories)).Union(d.GetFiles("*.png", SearchOption.AllDirectories))).ToArray(); //here i get all images 
            string str = "";
            ImageBL objBL = new ImageBL();
            foreach (FileInfo file in Files)
            {

 var InputFileName = Path.GetFileName(file.Name);
  string path = Server.MapPath("~/" + folderPath + "/");
  var RPaths = Server.MapPath("~/" + folderPath + "//" + "Images" + "//" + MD.YrID + "//" + MD.RPath);
 var ServerSavePath = Server.MapPath("~/" + folderPath + "//" + "Images" + "//" + MD.YrID + "//" + MD.RPath + "//" + InputFileName);


<pre>  if (!System.IO.Directory.Exists(RPaths))
                    {
                        System.IO.Directory.CreateDirectory(RPaths);
                    }

                    
                    if (System.IO.File.Exists(ServerSavePath))
                    {
                        ServerSavePath.Replace(ServerSavePath, System.DateTime.Now.ToLocalTime() + "" + ServerSavePath);
                    }
                    else
                    {
                        file.CopyTo(ServerSavePath);
                    }
}

推荐答案

你不能这样做。

它似乎在开发中工作,因为客户端和服务器都是同一台机器 - 但C#代码始终在服务器上运行,而不是在客户端上运行,因此代码中对用户硬盘的所有访问实际上都是对服务器硬盘的访问 - 因为它们是相同的物理机器,看起来它工作正常,但是一旦你发布到生产,客户端就在千里之外,服务器无法访问他的硬盘。



将文件从客户端上传到服务器的唯一方法是使用某种形式的上传控件,让用户准确指定他想要上传到你的文件。
You can't do it.
It appears to work in development because the client and the server are both the same machine - but C# code always runs on the server, never on the client, so all accesses to the "user HDD" in your code are actually accesses to the Server HDD - since they are the same physical machine it looks like it works fine, but as soon as you release to production the client is a thousand miles away and the server has no access to his HDD.

The only way to upload files from the client to the server is to use an upload control of some form and get the user to specify exactly which files he wants to upload to you.


这篇关于每次从客户端PC获取图像上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆