我正在尝试调整Kaltura API,以便它可以与 Windows 8.1 应用程序一起使用,但我遇到了这个函数的问题:
private void PostMultiPartWithFiles(HttpWebRequest request, KalturaParams kparams, KalturaFiles kfiles)
{
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
request.ContentType = "multipart/form-data; boundary=" + boundary;
// use a memory stream because we don't know the content length of the request when we have multiple files
MemoryStream memStream = new MemoryStream();
byte[] buffer;
int bytesRead = 0;
StringBuilder sb = new StringBuilder();
sb.Append("--" + boundary + "\r\n");
foreach (KeyValuePair<string, string> param in kparams)
{
sb.Append("Content-Disposition: form-data; name=\"" + param.Key + "\"" + "\r\n");
sb.Append("\r\n");
sb.Append(param.Value);
sb.Append("\r\n--" + boundary + "\r\n");
}
buffer = Encoding.UTF8.GetBytes(sb.ToString());
memStream.Write(buffer, 0, buffer.Length);
foreach (KeyValuePair<string, FileStream> file in kfiles)
{
sb = new StringBuilder();
FileStream fileStream = file.Value;
sb.Append("Content-Disposition: form-data; name=\"" + file.Key + "\"; filename=\"" + Path.GetFileName(fileStream.Name) + "\"" + "\r\n");
sb.Append("Content-Type: application/octet-stream" + "\r\n");
sb.Append("\r\n");
// write the current string builder content
buffer = Encoding.UTF8.GetBytes(sb.ToString());
memStream.Write(buffer, 0, buffer.Length);
// write the file content
buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
memStream.Write(buffer, 0, bytesRead);
buffer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
memStream.Write(buffer, 0, buffer.Length);
}
request.ContentLength = memStream.Length;
Stream requestStream = request.GetRequestStream();
// write the memorty stream to the request stream
memStream.Seek(0, SeekOrigin.Begin);
buffer = new Byte[checked((uint)Math.Min(4096, (int)memStream.Length))];
bytesRead = 0;
while ((bytesRead = memStream.Read(buffer, 0, buffer.Length)) != 0)
requestStream.Write(buffer, 0, bytesRead);
requestStream.Close();
memStream.Close();
}
我尝试过这样的事情:
private void PostMultiPartWithFiles(HttpWebRequest request, KalturaParams kparams, KalturaFiles kfiles)
{
using (request)
{
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
MultipartFormDataContent content = new MultipartFormDataContent(boundary);
foreach (KeyValuePair<string, string> param in kparams)
{
Stream fileStream = await new StringContent(param.Value).ReadAsStreamAsync();
StreamContent streamContent = new StreamContent(fileStream);
streamContent.Headers.Add("Content-Disposition", "form-data; name=\"" + param.Key + "\"");
content.Add(streamContent);
}
foreach (var file in kfiles)
{
StorageFile storageFile = await Windows.Storage.StorageFile.GetFileFromPathAsync(file.Value.Path);
var randomAccessStream = await storageFile.OpenReadAsync();
Stream stream = randomAccessStream.AsStreamForRead();
StreamContent streamContent = new StreamContent(stream);
content.Headers.Add("Content-Disposition", "form-data; name=\"" + file.Key + "\"; filename=\"" + file.Value.Name + "\"");
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
content.Add(streamContent, "fileData", file.Value.Path);
}
}
}
但它不起作用。它说fileData
没有附加。我以前没有真正使用过这些网络东西,所以也许我做错了。