Reputation: 1
I'm trying to use the ISAPI to upload a user to the face picture library, but I don't know where the mistake is. It always responds with an internal server error. Can you help me identify the issue?
below is my code
this is my razor page:
@page "/libraryUser"
@inject FaceLibraryUser FaceLibraryUser
@inject HttpClient Http
<h3>Send Face Data</h3>
<InputFile @ref="fileInput" OnChange="OnFileSelected" />
<button @onclick="SubmitData">Submit</button>
<p>@statusMessage</p>
@code {
private string statusMessage = "Please select an image.";
private IBrowserFile selectedFile;
private string selectedFileName;
private InputFile fileInput;
private void OnFileSelected(InputFileChangeEventArgs e)
{
selectedFile = e.File;
if (selectedFile != null)
{
selectedFileName = selectedFile.Name;
statusMessage = $"File selected: {selectedFile.Name}";
}
else
{
statusMessage = "No file selected.";
}
}
private async Task SubmitData()
{
if (selectedFile == null)
{
statusMessage = "Please select an image before submitting.";
return;
}
try
{
using var stream = selectedFile.OpenReadStream();
using var memoryStream = new MemoryStream();
await stream.CopyToAsync(memoryStream);
var imageBytes = memoryStream.ToArray();
// Call the function from the FaceLibraryUser service
var result = await FaceLibraryUser.UploadFaceToLibrary(2, "test", imageBytes, selectedFileName);
statusMessage = result;
}
catch (Exception ex)
{
statusMessage = $"Error: {ex.Message}";
}
}
}
this is my cs page:
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using System.Xml.Linq;
public class FaceLibraryUser
{
public static async Task<string> UploadFaceToLibrary(int fdid, string name, byte[] selectedImage, string fileName)
{
if (selectedImage == null || selectedImage.Length == 0)
{
return "Please select an image file first.";
}
var url = "http://192.168.2.64/ISAPI/Intelligent/FDLib/pictureUpload";
// Prepare the XML data
var xmlContent = new XElement("PictureUploadData",
new XElement("FDID", fdid),
new XElement("FaceAppendData",
new XElement("name", 1234))
).ToString();
// Convert the XML content to binary format
var xmlBytes = Encoding.UTF8.GetBytes(xmlContent);
var xmlBinaryContent = new ByteArrayContent(xmlBytes);
xmlBinaryContent.Headers.Add("Content-Disposition", "form-data; name=\"PictureUploadData\"");
// Create content for the image part
var imageContent = new ByteArrayContent(selectedImage);
imageContent.Headers.Add("Content-Disposition", $"form-data; name=\"importImage\"; filename=\"{fileName}\"");
imageContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
var boundary = GenerateCustomBoundary();
// Manually construct the multipart form data
using var content = new MultipartFormDataContent(boundary);
content.Add(xmlBinaryContent);
content.Add(imageContent);
// Set up HttpClient with HttpClientHandler for Basic Authentication
var handler = new HttpClientHandler
{
Credentials = new NetworkCredential("admin", "iDS-2CD7146G0-IZ") // Replace with your credentials
};
using var client = new HttpClient(handler);
client.DefaultRequestHeaders.Add("Cache-Control", "no-control");
client.DefaultRequestHeaders.Add("Connection", "keep-alive");
client.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest"); // Add X-Requested-With header
try
{
// Send the POST request
var response = await client.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
return "User added successfully.";
}
else
{
var errorResponse = await response.Content.ReadAsStringAsync();
return $"Failed to add user. Status Code: {response.StatusCode}, Reason: {errorResponse}";
}
}
catch (Exception ex)
{
return $"Error: {ex.Message}";
}
}
// Generate custom boundary: 26 dashes followed by 24 digits
private static string GenerateCustomBoundary()
{
var random = new Random();
var digits = new StringBuilder();
// Generate 24 random digits
for (int i = 0; i < 24; i++)
{
digits.Append(random.Next(0, 10)); // Random digit between 0 and 9
}
return new string('-', 26) + digits.ToString();
}
}
below is my error message:
Failed to add user. Status Code: InternalServerError, Reason: <?xml version="1.0" encoding="UTF-8"?> <ResponseStatus version="2.0" xmlns="http://www.hikvision.com/ver20/XMLSchema"> <requestURL></requestURL> <statusCode>3</statusCode> <statusString>Device Error</statusString> <subStatusCode>deviceError</subStatusCode> <description>deviceError. The HTTP request is abnormal. Please first confirm whether the device version is supported and whether the request content conforms to the protocol standard, or contact technical support to provide help. Thank you;</description> </ResponseStatus>
error image : errorMessage
i hope can find the error and solution and can success upload user to face picture library
Upvotes: 0
Views: 56