[英]How to send post request from c# windows forms to a laravel api with files?
我有一個 windows 表單,它將向我的簡單 laravel api 發送一個發布請求。
這是我到目前為止所做的:
private async void UploadScreenshot(string employee_id, string filepath)
{
string url = "http://127.0.0.1:8000/api/upload-screenshot";
using (HttpClient client = new HttpClient())
{
var multiForm = new MultipartFormDataContent();
Image ssimage = Image.FromFile(filepath);
var byteimg = ImageToByteArray(ssimage);
ByteArrayContent screenshotimg = new ByteArrayContent(byteimg);
multiForm.Add(new ByteArrayContent(byteimg), "files");
multiForm.Add(new StringContent(employee_id), "emp_number");
var response = await client.PostAsync(url, multiForm);
var responseString = await response.Content.ReadAsStringAsync();
System.Windows.MessageBox.Show(responseString);
//Console.WriteLine(responseString);
}
}
此代碼僅適用於StringContent
。但在我的ByteArrayContent
中,它返回 null。 使用MultipartFormDataContent
的事件。 從昨天開始,我在 Stackoverflow 中嘗試了所有答案,但對我來說沒有運氣。
這是我的 Laravel API controller:
public function UploadScreenshot(Request $request){
$emp_number = $request->emp_number; //this is working..
$file = $request->files; //this is null
if ($request->hasFile('screenshot')) {
$file = request()->file('files');
$fname = date('YmdHis');
$ext = $request->files->extension();
$filename = $fname . '.' . $ext;
$file->storeAs('screenshots/', $filename, ['disk' => 'private']);
}
}
謝謝。
這就是我所做的並且像魅力一樣工作:
在 PrintScreen 方法中,我首先將文件保存到臨時目錄。 然后調用我的UploadScreenshot()
方法,
private void PrintScreen()
{
DateTime utc = DateTime.UtcNow;
string filename = emp_number + " - " + utc.Ticks.ToString() + ".png";
Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(printscreen as Image);
graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
printscreen.Save(runnitsettings + filename, ImageFormat.Png);
UploadScreenshot(emp_number, runnitsettings + filename);
}
在這個 function 中,我從目錄中檢索文件並將其加載到FileStream
,而不是直接將文件添加到MultipartFormDataContent();
我創建了一個 function ImageToByte()
,它將首先將文件轉換為byte
,然后再轉換為Base64String
。
private void UploadScreenshot(string employee_id, string filepath)
{
string url = baseurl + "upload-screenshot";
System.IO.FileStream fsstream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
string b64image = ImageToByte(fsstream);
fsstream.Dispose();
File.Delete(filepath);
//I deleted the file after convertion to free some diskspace.
var multiForm = new MultipartFormDataContent();
multiForm.Add(new StringContent(employee_id), "emp_number");
multiForm.Add(new StringContent(b64image), "files");
//Then I post the data:
var response = await client.PostAsync(url, multiForm);
var responseString = await response.Content.ReadAsStringAsync();
}
圖像到字節 Function:
public static string ImageToByte(FileStream fs)
{
byte[] imgBytes = new byte[fs.Length];
fs.Read(imgBytes, 0, Convert.ToInt32(fs.Length));
string encodeData = Convert.ToBase64String(imgBytes, Base64FormattingOptions.InsertLineBreaks);
return encodeData;
}
在我的 Laravel Controller 中,我這樣處理文件:
$destinationpath = config('filesystems.disks')["private"]["root"]."/screenshots"."/";
$emp_number = $request["emp_number"];
$screenshot = $request["files"];
$filename = $emp_number.'-'.date('YmdHis');
$filePath = $destinationpath.$filename.".png";
file_put_contents($filePath, base64_decode($screenshot));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.