Answered by:
Need to pass files from a folder and their details to an API dynamically

Question
-
User-1660589204 posted
I am working on a program which passes file details to an API and gets an ID. I am confused on how can I collect the values and set them as parameters to my API. API takes one file details at a time where as the source folder could contain one or many media files.
public static void GetFilesID(HttpClient httpclient, string result) { HttpClient client = new HttpClient(); DirectoryInfo info = new DirectoryInfo(path); info.GetFiles("*.trm"); DataTable FileDetailsTable = new DataTable(); System.Net.Http.HttpResponseMessage response = client .PostAsync(".../api/v2/file/uploader", fileDetails); response.EnsureSuccessStatusCode(); }
All the files I get are media files.
My API takes four parameters as: FilePath, FileName, FileType, FileReferenceID(generated from DB). I have a model fileDetails which contains all the above parameters and some more columns. How can I pass these file details to DB save them and pass the saved values into API. I would like to use Linq for DB communication. Please help me with this.
Tuesday, November 10, 2020 7:59 AM
Answers
-
User-939850651 posted
Hi ddesarajubyc,
According to your description, I think you may need the FileInfo Class to accept the file information that meets the filter, and then you could encapsulate this information into a custom fileDetails model object, and then serialize it as a parameter to your API.
Something like this:
DirectoryInfo info = new DirectoryInfo(path); List<FileDetails> details = new List<FileDetails>(); FileInfo[] fileInfos = info.GetFiles("*.trm"); if (fileInfos != null) { foreach (FileInfo fileInfo in fileInfos) { FileDetails detail = new FileDetails(); detail.FileName = fileInfo.Name; detail.FilePath = fileInfo.FullName; detail.FileType = fileInfo.Extension; details.Add(detail); } } var fileDetails = JsonConvert.SerializeObject(details); HttpClient client = new HttpClient(); string url = "your api address"; HttpContent httpContent = new StringContent(fileDetails, Encoding.UTF8, "application/json"); var response = client.PostAsync(url, httpContent);
Hope this can help you.
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, November 11, 2020 5:47 AM
All replies
-
User-939850651 posted
Hi ddesarajubyc,
According to your description, I think you may need the FileInfo Class to accept the file information that meets the filter, and then you could encapsulate this information into a custom fileDetails model object, and then serialize it as a parameter to your API.
Something like this:
DirectoryInfo info = new DirectoryInfo(path); List<FileDetails> details = new List<FileDetails>(); FileInfo[] fileInfos = info.GetFiles("*.trm"); if (fileInfos != null) { foreach (FileInfo fileInfo in fileInfos) { FileDetails detail = new FileDetails(); detail.FileName = fileInfo.Name; detail.FilePath = fileInfo.FullName; detail.FileType = fileInfo.Extension; details.Add(detail); } } var fileDetails = JsonConvert.SerializeObject(details); HttpClient client = new HttpClient(); string url = "your api address"; HttpContent httpContent = new StringContent(fileDetails, Encoding.UTF8, "application/json"); var response = client.PostAsync(url, httpContent);
Hope this can help you.
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, November 11, 2020 5:47 AM -
User-1660589204 posted
This is good code for my implementation XuDong Peng,
I need one modification though. My FileDetails Model has following properties in it:
public partial class FileDetails { public string FileUrl { get; set; } public string Filename { get; set; } public string Case_ID { get; set; } public string FileType { get; set; } public Int64 File_Reference_Id { get; set; } public DateTime Created_Date { get; set; } public DateTime Updated_Date { get; set; } public string Status { get; set; } public string File_Id { get; set; } public string Preset { get; set; } public string Transcode_callbackUrl { get; set; } public string FileTranscodeJob_Id { get; set; } public string Transcription_callbackUrl { get; set; } public string jobDefinition { get; set; } public string TranscriptionJob_Id { get; set; } }
I am unable to set these values via fileInfo type. I need to set all these values in the foreach loop. My implementation as suggested by you shows compile time error:
public Task<FileDetails> UploadFiles(string filename) { DirectoryInfo info = new DirectoryInfo(path); List<FileDetails> details = new List<FileDetails>(); FileInfo[] fileInfos = info.GetFiles("*.trm"); if (fileInfos != null) { foreach (FileInfo fileInfo in fileInfos) { FileDetails detail = new FileDetails(); detail.FileUrl = fileInfo.Name; detail.Filename = fileInfo.FullName; detail.FileType = fileInfo.Extension; details.Add(detail); } }
I need to assign the exact values for my coding. Is there any operation for this. Please let me know.
Wednesday, November 11, 2020 12:28 PM