0

嗨,我想使用带有 ASp.net (4.0)、c# 的 box api 2.0 在框中上传文件。我收到 400(错误请求错误)。

以下是我的代码。

`public bool Upload(byte[] FileBytes, string folderId, string FileName) { UploadFileBytes = FileBytes; 上传文件名 = 文件名;

    UploadSplitFile(folderId);
    return true;

}


public void UploadSplitFile(string FolderId)
{
    string Filename = UploadFileName;
    byte[] buffer;
    using (MemoryStream resultStream = new MemoryStream())
    {
        buffer = AssembleFilesBlock();
        resultStream.Write(buffer, 0, buffer.Length);
        buffer = GetFormattedBoundary(true);
        resultStream.Write(buffer, 0, buffer.Length);
        resultStream.Flush();
        buffer = resultStream.ToArray();
    }
    HttpWebRequest myRequest = CreateRequest(buffer.Length, FolderId);
    using (Stream newStream = myRequest.GetRequestStream())
    {
        newStream.Write(buffer, 0, buffer.Length);
        newStream.Close();
    }
    string response;

    using (HttpWebResponse myHttpWebResponse = (HttpWebResponse)myRequest.GetResponse())
    {
        using (Stream responseStream = myHttpWebResponse.GetResponseStream())
        {
            TextReader reader = new StreamReader(responseStream);

            response = reader.ReadToEnd();
        }
    }
}
private byte[] AssembleFile()
{
    byte[] buffer;
    using (MemoryStream resultStream = new MemoryStream())
    {
        buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"; parent_id=\"0\"{2}", Guid.NewGuid(), Path.GetFileName(UploadFileName), Environment.NewLine));
        resultStream.Write(buffer, 0, buffer.Length);
        buffer = Encoding.ASCII.GetBytes("Content-Type: application/octet-stream" + Environment.NewLine + Environment.NewLine);
        resultStream.Write(buffer, 0, buffer.Length);            
        buffer = UploadFileBytes;
        resultStream.Write(buffer, 0, buffer.Length);
        buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
        resultStream.Write(buffer, 0, buffer.Length);
        resultStream.Flush();
        buffer = resultStream.ToArray();
    }
    return buffer;
}

private byte[] AssembleFilesBlock()
{
    byte[] buffer;

    using (MemoryStream resultStream = new MemoryStream())
    {
        buffer = GetFormattedBoundary(false);
        resultStream.Write(buffer, 0, buffer.Length);

        buffer = AssembleFile();
        resultStream.Write(buffer, 0, buffer.Length);

        resultStream.Flush();
        buffer = resultStream.ToArray();
    }

    return buffer;
}

private byte[] GetFormattedBoundary(bool isEndBoundary)
{
    string template = isEndBoundary ? "--{0}--{1}" : "--{0}{1}";
    return Encoding.ASCII.GetBytes(string.Format(template, "AaBbCcX30", Environment.NewLine));
}

private HttpWebRequest CreateRequest(int contentLength, string FolderId)
{
    HttpWebRequest webRequest;
    webRequest = (HttpWebRequest)WebRequest.Create("https://upload.box.com/api/2.0/files/content");

    webRequest.Method = "POST";
    webRequest.AllowWriteStreamBuffering = true;
    webRequest.ContentType = string.Concat("multipart/form-data;boundary=", "AaBbCcX30");
    webRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
    webRequest.Headers.Add("Accept-Charset", "ISO-8859-1");
    webRequest.Headers.Add("Authorization", "Bearer Access Token");
    webRequest.ContentLength = contentLength;
    return webRequest;
}`
4

1 回答 1

1

试试这个代码。

我没有使用 John Hoerr 的 SDK,它只使用 RestSharp dll 和http://developers.box.com/docs/#files-upload-a-file中给出的上传 api 。

参数 folder_id 是您要将文件上传到的文件夹的 ID。

public void Upload_Doc(string folder_id,string accessToken)
        {
            var client = new RestClient("https://upload.box.com/api/2.0");
            var request = new RestRequest("files/content", Method.POST);
            request.AddParameter("parent_id", folder_id); 

            request.AddHeader("Authorization", "Bearer " + accessToken);

            string path = @"C:\Users\xyz\Desktop\homepage.png";
            byte[] byteArray = System.IO.File.ReadAllBytes(path);

            request.AddFile("filename", byteArray, "homepage.png");

            var responses = client.Execute(request);
            var content = responses.Content; 
        }
于 2013-12-02T12:23:21.137 回答