如何在 IIS(Internt Information Services) 的 WebService 中使用 FileStream 上传文件(大尺寸)

发布于 2024-11-08 11:07:41 字数 255 浏览 1 评论 0原文

我正在使用 C# 创建文件上传服务。 我创建了三种方法:

  • upload_start(字符串文件名)
  • upload_continue(字符串文件名,byte[]缓冲区)
  • upload_end(字符串文件名)
  • 它可以工作,但我不想处理客户端程序中的 3 个函数。如何从客户端程序打开一个FileStream,并让服务器端完成文件上传?

    I am using C# to create a service for file uploading.
    I created three methods:

  • upload_start(string filename)
  • upload_continue(string filename, byte[] buffer)
  • upload_end(string filename)
  • It works, but I don't want to handle 3 functions from the client program. How can I open a FileStream from the client program, and let the server side finish the file upload?

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(3

    清秋悲枫 2024-11-15 11:07:42

    最简单的方法可能是使用 wcf 或使用 asp.net 执行类似操作来创建 REST 服务,但您只需执行 POST 操作,服务器就可以完成这项工作。

    这是一种方法:

    http://debugmode.net/2011/05/01/uploading-file-to-server-from-asp-net-client-using-wcf-rest-service/

    这将有为您提供更简单的界面。

    The simplest approach would probably be to create a REST service, either with wcf or do something similar with asp.net, but you would then just do a POST operation, and the server could do the work.

    Here is one approach:

    http://debugmode.net/2011/05/01/uploading-file-to-server-from-asp-net-client-using-wcf-rest-service/

    This would have a simpler interface for you.

    街角迷惘 2024-11-15 11:07:42

    我认为你不能,因为 FileStream 不可序列化。为什么不向服务传递文件名(就像您已经是的那样)并让服务打开文件并处理它。

    I don't think you can as the FileStream is not Serializable. Why not pass the service the file name (as you already are) and have the service open the file and process it.

    缱绻入梦 2024-11-15 11:07:42

    要在 WebServices(不是 Windows Cominication Foundation WCF)中上传大文件,请遵循以下方法:

    在服务器端文件 Web.Config 中添加此 xml 并根据上传的最大大小更改 maxRequestLength 的值,默认 maxRequestLength 为4MB,在本例中8192=MB

        <httpRuntime
            executionTimeout="600"
            maxRequestLength="8192"
            useFullyQualifiedRedirectUrl="false"
            minFreeThreads="8"
            minLocalRequestFreeThreads="4"
            appRequestQueueLimit="100"
            enableVersionHeader="true"
      />
    

    在服务器端添加另一个公共函数,该函数接收一个字节[],其中包含文件、文件名和服务器中不会​​保存文件的路径。

     public String UploadFile(byte[] fileByte, String fileName, String savePath)
            {
    
                string newPath = savePath;
    
    
                if (!Directory.Exists(newPath))
                {
                    Directory.CreateDirectory(newPath);
                }
    
                newPath = newPath + fileName;
    
                System.IO.FileStream fs1 = null;
                byte[] b1 = null;
                b1 = fileByte;
                fs1 = new FileStream(newPath, FileMode.Create);
                fs1.Write(b1, 0, b1.Length);
                fs1.Flush();
                fs1.Close();
                fs1 = null;
    
                return newPath;
            }
    

    在客户端添加以下代码来发送文件:

    Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
                dlg.FileName = "Select a File"; 
                dlg.DefaultExt = ".xls";  
                dlg.Filter = "Excel documents (.xls)|*.xls";  
    
                // Show open file dialog box 
                Nullable<bool> result = dlg.ShowDialog();
    
                // Process open file dialog box results 
                if (result == true)
                {
    
                    string filename = dlg.FileName;
    
    
                    string file = Path.GetFileName(filename);
    
                    System.IO.FileStream fs1 = null;
                    fs1 = System.IO.File.Open(filename, FileMode.Open, FileAccess.Read);
                    byte[] b1 = new byte[fs1.Length];
                    fs1.Read(b1, 0, (int)fs1.Length);
                    fs1.Close();
    
    
                    String savePath = @"C:\DOC\IMPORT\";
                    String newPath = WEB_SERVICES.UploadFile(b1, file, savePath);
    
    
                    MessageBox.Show(String.Format("The file is uploaded in {0}", newPath));
    
    
                }
                else
                {
                    MessageBox.Show("Select a file, please");
                }
    

    For upload a large file in WebServices(not Windows Cominication Foundation WCF) this is the way to follow:

    In the server side file Web.Config add this xml and change the value of maxRequestLength depending on the maximum size of upload, by defoult maxRequestLength is 4MB, in this example 8192=MB

        <httpRuntime
            executionTimeout="600"
            maxRequestLength="8192"
            useFullyQualifiedRedirectUrl="false"
            minFreeThreads="8"
            minLocalRequestFreeThreads="4"
            appRequestQueueLimit="100"
            enableVersionHeader="true"
      />
    

    Add another public function in server side, this function received a byte[] contains the file, the file name and a path where you wont save the file in the server.

     public String UploadFile(byte[] fileByte, String fileName, String savePath)
            {
    
                string newPath = savePath;
    
    
                if (!Directory.Exists(newPath))
                {
                    Directory.CreateDirectory(newPath);
                }
    
                newPath = newPath + fileName;
    
                System.IO.FileStream fs1 = null;
                byte[] b1 = null;
                b1 = fileByte;
                fs1 = new FileStream(newPath, FileMode.Create);
                fs1.Write(b1, 0, b1.Length);
                fs1.Flush();
                fs1.Close();
                fs1 = null;
    
                return newPath;
            }
    

    In the client side add this code to send the file:

    Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
                dlg.FileName = "Select a File"; 
                dlg.DefaultExt = ".xls";  
                dlg.Filter = "Excel documents (.xls)|*.xls";  
    
                // Show open file dialog box 
                Nullable<bool> result = dlg.ShowDialog();
    
                // Process open file dialog box results 
                if (result == true)
                {
    
                    string filename = dlg.FileName;
    
    
                    string file = Path.GetFileName(filename);
    
                    System.IO.FileStream fs1 = null;
                    fs1 = System.IO.File.Open(filename, FileMode.Open, FileAccess.Read);
                    byte[] b1 = new byte[fs1.Length];
                    fs1.Read(b1, 0, (int)fs1.Length);
                    fs1.Close();
    
    
                    String savePath = @"C:\DOC\IMPORT\";
                    String newPath = WEB_SERVICES.UploadFile(b1, file, savePath);
    
    
                    MessageBox.Show(String.Format("The file is uploaded in {0}", newPath));
    
    
                }
                else
                {
                    MessageBox.Show("Select a file, please");
                }
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文