我在上傳一個檔案(本例中為圖片)和一個JSON字串時遇到了一個問題。我作為客戶端,試圖上傳至網路服務器。目前在網站上,當上傳一個檔案時,有效載荷是這樣的:
。------WebKitFormBoundary1A4Toq4hnrayCRu4
Content-Disposition: form-data; name="FileInstance"/span>
{"action":["/public/my_folder"],"fileName":"download_icon"。 png","fileSize":313,"certification": {"level":"0","Groups":[]}}。
------WebKitFormBoundary1A4Toq4hnrayCRu4
Content-Disposition: form-data; name="download_icon.png"/span>; filename="download_icon.png"/span>
Content-Type: image/png
------WebKitFormBoundary1A4Toq4hnrayCRu4--
我通過2個單獨的請求進行POST,每個結果都有一個200狀態代碼,但查看結果,它是空的,我應該收到一個上傳檔案的md5hash,但我沒有。
這是我的代碼:
//先上傳 JSON
MultipartFormDataContent form = new MultipartFormDataContent()。
var boundary = $"----WebKitFormBoundary" DateTime.Now.Ticks.ToString("x");
form.Headers.Remove("Content-Type")。
form.Headers.Add("Content-Type", $"multipart/form-data; boundary={boundary}") 。
MyFileClass currentFile = new MyFileClass()。
currentFile.action = new List<string>() { "/public/my_folder"/span> };
currentFile.filename = Path.GetFileName(Filename);
currentFile.fileSize = Convert.ToInt32(new FileInfo(Filename).Length)。
currentFile.certification= new MyFileClass.Certification()。
CreateCertification(currentFile.certification)。
var json = JsonConvert.SerializeObject(currentFile);
HttpContent content = new StringContent(json, Encoding.UTF8, "application/json") 。
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
名稱 = Path.GetFileName(Filename)。
FileName = Path.GetFileName(Filename)
};
var response = await myHttpClient.PostAsync(url, form);
var result_str = response.Content.ReadAsStringAsync().Result;
//上傳實際檔案
var stream = new FileStream(Filename, FileMode.Open) 。
form = new MultipartFormDataContent()。
boundary = $"----WebKitFormBoundary" DateTime.Now.Ticks.ToString("x")。
form.Headers.Remove("Content-Type")。
form.Headers.Add("Content-Type", $"multipart/form-data; boundary={boundary}") 。
content = new StreamContent(stream)。
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
名稱 = "FileInstance"
};
content = new StreamContent(stream)。
form.Add(content, Path.GetFileNameWithoutExtension(Filename))。
response = await myHttpClient.PostAsync(url, form);
result_str = response.Content.ReadAsStringAsync().Result。
編輯1:這是httpclient的定義方式:
編輯2:這是httpclient的定義方式。
string password = SecureStringExtensions.ToUnsecuredString(Password)。
var credCache = new CredentialCache
{
{
new Uri(url), "Basic", new NetworkCredential(Username, password)
}
};
var myHttpClient = new HttpClient(new HttpClientHandler() { Credentials = credCache }) 。
myHttpClient.DefaultRequestHeaders.Add("Connection", "keep-alive") 。
myHttpClient.Timeout = Timeout.InfiniteTimeSpan。
uj5u.com熱心網友回復:
我一直在嘗試不同的POSTing方法,但都沒有成功。現在我找到了一個解決方案,我洗掉了對邊界和ContentType的編輯,直接將檔案和JSON同時添加到MultipartFormDataContent中,并且運行良好。
MyFileClass currentFile = new MyFileClass();
currentFile.action = new List<string>() { "/public/my_folder"/span> };
currentFile.filename = Path.GetFileName(Filename);
currentFile.fileSize = Convert.ToInt32(new FileInfo(Filename).Length)。
currentFile.certification= new MyFileClass.Certification()。
CreateCertification(currentFile.certification)。
var json = JsonConvert.SerializeObject(currentFile);
var formContent = new MultipartFormDataContent
{
{ new StringContent(json, Encoding.UTF8, "application/json"/span>) },
{ new StreamContent(new MemoryStream(File.ReadAllBytes(Filename))), Path.GetFileName(Filename) ,Path.GetFileName(Filename)}.
};
var response = await myHttpClient.PostAsync(url, formContent)。
string stringContent = await response.Content.ReadAsStringAsync()。
其中Filename是檔案本身的絕對路徑。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/312422.html
標籤:
