Ich versuche, die Web-API-Methode zum Speichern der Dateidaten aufzurufen.Wenn ich Webapi-Methode debuggen fand ich, dass ContentLength nicht als richtig kommt, aus diesem Grund, wenn ich die Datei abrufen, zeigt es Fehler als beschädigte Datei.Wie übermittele ich die Dateidaten von der C# -Konsolenanwendung an WebApi?
meine Klasse Methode ist: -
using (var formData = new MultipartFormDataContent())
{
HttpContent stringContent = new StringContent(file);
formData.Add(stringContent, "file", file);
formData.Add(new StringContent(JsonConvert.SerializeObject(file.Length)), "ContentLength ");
HttpResponseMessage responseFile = client.PostAsync("Report/SaveFile?docId=" + docId, formData).Result;
}
My Web api Methode ist: -
[HttpPost]
public HttpResponseMessage SaveFile(long docId)
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Unauthorized);
try
{
var httpRequest = HttpContext.Current.Request;
bool IsSuccess = true;
if (httpRequest.Files.Count > 0)
{
var docfiles = new List<string>();
foreach (string file in httpRequest.Files)
{
HttpPostedFile postedFile = httpRequest.Files[file];
// Initialize the stream.
Stream myStream = postedFile.InputStream;
myStream.Position = 0;
myStream.Seek(0, SeekOrigin.Begin);
var _item = CorrectedReportLibrary.Services.ReportService.SaveFile(myStream,docId);
response = Request.CreateResponse<bool>((IsSuccess)
? HttpStatusCode.OK
: HttpStatusCode.NoContent,
IsSuccess);
}
}
}
catch (Exception ex)
{
Theranos.Common.Library.Util.LogManager.AddLog(ex, "Error in CorrectedReportAPI.Controllers.SaveDocument()", null);
return Request.CreateResponse<ReportDocumentResult>(HttpStatusCode.InternalServerError, null);
}
return response;
}
Wie kann ich die ContentLength
von C# Klassenmethode eingestellt?
Below-Code ich habe, wie Sie gesagt, aber immer noch gleiche Problem kommt, dass ids Datei beschädigt ist ed: contents.Add (neuer StreamContent (stre), "Datei", Datei); var result = erwarten client.PostAsync ("ReportInfo/SaveFile? DocId =" + docId, Inhalt); – jack123
@gorakh Mit serverseitigem Beispiel aktualisiert – Hypnobrew