.NET Framework
अपलोड फ़ाइल और POST डेटा वेबसर्वर के लिए
खोज…
WebRequest के साथ फाइल अपलोड करें
एकल अनुरोध में फ़ाइल और फ़ॉर्म डेटा भेजने के लिए, सामग्री में मल्टीपार्ट / फ़ॉर्म-डेटा प्रकार होना चाहिए।
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
public async Task<string> UploadFile(string url, string filename,
Dictionary<string, object> postData)
{
var request = WebRequest.CreateHttp(url);
var boundary = $"{Guid.NewGuid():N}"; // boundary will separate each parameter
request.ContentType = $"multipart/form-data; {nameof(boundary)}={boundary}";
request.Method = "POST";
using (var requestStream = request.GetRequestStream())
using (var writer = new StreamWriter(requestStream))
{
foreach (var data in postData)
await writer.WriteAsync( // put all POST data into request
$"\r\n--{boundary}\r\nContent-Disposition: " +
$"form-data; name=\"{data.Key}\"\r\n\r\n{data.Value}");
await writer.WriteAsync( // file header
$"\r\n--{boundary}\r\nContent-Disposition: " +
$"form-data; name=\"File\"; filename=\"{Path.GetFileName(filename)}\"\r\n" +
"Content-Type: application/octet-stream\r\n\r\n");
await writer.FlushAsync();
using (var fileStream = File.OpenRead(filename))
await fileStream.CopyToAsync(requestStream);
await writer.WriteAsync($"\r\n--{boundary}--\r\n");
}
using (var response = (HttpWebResponse) await request.GetResponseAsync())
using (var responseStream = response.GetResponseStream())
{
if (responseStream == null)
return string.Empty;
using (var reader = new StreamReader(responseStream))
return await reader.ReadToEndAsync();
}
}
उपयोग:
var response = await uploader.UploadFile("< YOUR URL >", "< PATH TO YOUR FILE >",
new Dictionary<string, object>
{
{"Comment", "test"},
{"Modified", DateTime.Now }
});
Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow