Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将文件上载到服务器-c#_C#_File Upload - Fatal编程技术网

将文件上载到服务器-c#

将文件上载到服务器-c#,c#,file-upload,C#,File Upload,我构建了一个客户端-服务器应用程序,用于将文件从客户端文件夹上载到服务器。 我的服务器上传方法如下- [WebMethod] public string UploadFile(byte[] f, string fileName) { // the byte array argument contains the content of the file // the string argument contains the name and extension // of

我构建了一个客户端-服务器应用程序,用于将文件从客户端文件夹上载到服务器。 我的服务器上传方法如下-

[WebMethod]
public string UploadFile(byte[] f, string fileName)
{
    // the byte array argument contains the content of the file
    // the string argument contains the name and extension
    // of the file passed in the byte array
    new general().logError("UploadFile " + fileName);
    try
    {
        // instance a memory stream and pass the
        // byte array to its constructor
        MemoryStream ms = new MemoryStream(f);

                   FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath
                    ("~/data/") + fileName, FileMode.Create);

        // write the memory stream containing the original
        // file as a byte array to the filestream
        ms.WriteTo(fs);

        // clean up
        ms.Close();
        fs.Close();
        fs.Dispose();
        new general().logError("After saving the file");
        // return OK if we made it this far
        return "OK";
    }
    catch (Exception ex)
    {

        return ex.Message.ToString();
    }
}
private void uploadIt(string fName)
    {
        FileStream f = File.OpenRead(fName);

        cloudius.cloudius m = new cloudius.cloudius();

        using (MemoryStream ms = new MemoryStream())
        {
            f.CopyTo(ms);

            //string[] drive = fName.Split(':');
            string[] p = fName.Split('\\');

            string b = m.UploadFile(ms.ToArray(), p[p.Length - 1]); // 

        }
    }
调用此WebMethod的函数如下-

[WebMethod]
public string UploadFile(byte[] f, string fileName)
{
    // the byte array argument contains the content of the file
    // the string argument contains the name and extension
    // of the file passed in the byte array
    new general().logError("UploadFile " + fileName);
    try
    {
        // instance a memory stream and pass the
        // byte array to its constructor
        MemoryStream ms = new MemoryStream(f);

                   FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath
                    ("~/data/") + fileName, FileMode.Create);

        // write the memory stream containing the original
        // file as a byte array to the filestream
        ms.WriteTo(fs);

        // clean up
        ms.Close();
        fs.Close();
        fs.Dispose();
        new general().logError("After saving the file");
        // return OK if we made it this far
        return "OK";
    }
    catch (Exception ex)
    {

        return ex.Message.ToString();
    }
}
private void uploadIt(string fName)
    {
        FileStream f = File.OpenRead(fName);

        cloudius.cloudius m = new cloudius.cloudius();

        using (MemoryStream ms = new MemoryStream())
        {
            f.CopyTo(ms);

            //string[] drive = fName.Split(':');
            string[] p = fName.Split('\\');

            string b = m.UploadFile(ms.ToArray(), p[p.Length - 1]); // 

        }
    }
当运行aboce代码时,我得到以下错误-

客户端发现响应内容类型为“text/html”,但应为“text/xml”。


你知道是什么导致了这个错误吗?

经过一些研究后,从外观上看,这似乎是一种错误页面返回的形式。你也去看看吧


希望这能为您的问题提供某种形式的澄清。

嘿,伙计,如果您的方法的主要目的只是上传一个您可以使用的文件:

FileUpload fu;  // Get the FileUpload object.
using (FileStream fs = File.OpenWrite("file.dat"))
{
    fu.PostedFile.InputStream.CopyTo(fs);
    fs.Flush();
}

这将更加有效,因为您将直接将输入文件流式传输到目标主机,而不首先在内存或磁盘上缓存。

您是否也会收到http 500错误代码?当您单步执行此操作时,会发生什么情况?您是否有异常?堆栈跟踪显示什么?@S.Dav Yes,我现在可以看到,我得到500-内部服务器错误。我需要上传文件夹树从Windows项目。不确定是否可以为此目的使用FileUpload控件。