Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.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# asp.NETC中的文件上载#_C#_Asp.net_Asp.net 3.5 - Fatal编程技术网

C# asp.NETC中的文件上载#

C# asp.NETC中的文件上载#,c#,asp.net,asp.net-3.5,C#,Asp.net,Asp.net 3.5,嘿,伙计们,我正在使用一个api,下面是上传api的代码 public string Upload(string uploadUrl, NameValueCollection args, string filePath) { _queryString = args; //no required args WebClient client = createWebClient(); _queryString["api_format"]

嘿,伙计们,我正在使用一个api,下面是上传api的代码

   public string Upload(string uploadUrl, NameValueCollection args, string filePath) 
   {
        _queryString = args; //no required args

        WebClient client = createWebClient();
        _queryString["api_format"] = APIFormat ?? "xml"; //xml if not specified - normally set in required args routine                                   
        queryStringToArgs();

        string callUrl = _apiURL + uploadUrl + "?" + _args;
        callUrl = uploadUrl + "?" + _args;

        try {
             byte[] response = client.UploadFile(callUrl, filePath);
             return Encoding.UTF8.GetString(response);     
        } catch {
            return "";
        }   
    }
下面是我上传文件的代码,我使用FileUpload控件获取文件的完整路径(但我没有成功)


我在一些论坛上读到,出于某种安全目的,您无法从客户端获取文件的完整路径。如果这是真的,那么在我的场景中如何实现文件上传?

是的,这是真的,出于安全原因,您无法获取客户端计算机的完整路径,您可以做的是,尝试以下操作

      Stream stream = fileUpload.PostedFile.InputStream;
      stream.Read(bytes, 0, fileUpload.PostedFile.ContentLength);

使用FileUploadControl提供的流,而不是创建自己的文件流。Hoep它会有帮助。

什么是.NET版本?是普通的旧ASP.NET还是ASP.NET MVC?请在标记中添加所有内容。我可以使用
而不是
文件流
,但是API方法
上载
将文件路径作为其第三个参数,如果您有“bytes”byte[],我如何为它提供完整的文件路径,您可以将其保存在Web服务器上的某个临时位置,并提供该控件的路径。@Furqn:thanx dude,它起作用了,但是否可以将该字节[]存储在windows临时文件夹中?稍后删除?是的,这是可能的,但不推荐,您应该在某个特定于应用程序的临时文件夹中执行此操作,因为您必须为asp.net用户或您为保存文件而模拟的任何其他用户授予权限。
      Stream stream = fileUpload.PostedFile.InputStream;
      stream.Read(bytes, 0, fileUpload.PostedFile.ContentLength);