locked
read ID3 tags with out uploading complete audio file RRS feed

  • Question

  • User-1399352090 posted

    Hello,

    i'm uploading audio file using multiparts(chunk) using web api. and seems everything OK. 

    but i have another requirement that  i have to show ID3 tags like(title, album, image, ect....) before upload.

    i don' t want to wait until 100MB file upload completely then return id3 tags.

    i want to return Id3 data initial (first)chunk data uploaded is there any ways to return id3 data on first chunk downloaded. 

    my code:

    public class UploadTrackController : ApiController
        {
    
            [HttpPost]
            [Route("")]
            public async Task<IHttpActionResult> upload()
            {
    
                var root = HttpContext.Current.Server.MapPath("~/App_Data/Tmp/FileUploads");
                // Check if the request contains multipart/form-data.
                if (!Request.Content.IsMimeMultipartContent("form-data"))
                {
                    return BadRequest("Unsupported media type");
                }
                try
                {
                    var provider = new CustomMultipartFormDataStreamProvider(root);
                    //await Request.Content.ReadAsMultipartAsync(provider);
                    await Task.Run(async () => await Request.Content.ReadAsMultipartAsync(provider));
    
                    ///  var photos = new List<PhotoViewModel>();
    
                    foreach (var file in provider.FileData)
                    {
                        var fileInfo = new FileInfo(file.LocalFileName);
                        // we can do all next steps
                        //Audio.Add(new AudioViewModel
                        //{
                        //    Name = fileInfo.Name,
                        //    Created = fileInfo.CreationTime,
                        //    Modified = fileInfo.LastWriteTime,
                        //    Size = fileInfo.Length / 1024
                        //});
                    }
                    return Ok(new { Message = "Photos uploaded ok" });
                }
                catch (Exception ex)
                {
                    return BadRequest(ex.GetBaseException().Message);
                }
            }
    
            /// <summary>
            ///   Check if file exists on disk
            /// </summary>
            /// <param name="fileName"></param>
            /// <returns></returns>
            public bool FileExists(string fileName)
            {
                var file = Directory.GetFiles("~/App_Data/Tmp/FileUploads", fileName)
                    .FirstOrDefault();
    
                return file != null;
            }
        }
       
        public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
        {
            public CustomMultipartFormDataStreamProvider(string rootPath) : base(rootPath)
            {
            }
    
            public CustomMultipartFormDataStreamProvider(string rootPath, int bufferSize) : base(rootPath, bufferSize)
            {
            }
    
            public override string GetLocalFileName(HttpContentHeaders headers)
            {
                //Make the file name URL safe and then use it & is the only disallowed url character allowed in a windows filename
                var name = !string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName)
                  ? headers.ContentDisposition.FileName
                  : "NoName";
                name= name.Trim('"').Replace("&", "and");
                return  Utilites.GenerateFileName(name.ToString());
                 
            }
        }

    Appreciated for quick and best response 

    Sunday, October 1, 2017 1:29 PM

Answers

  • User-474980206 posted
    As you code require html file I/o, why not just read the I’d tags with JavaScript. Then now upload is required to parse. There are a lot of libraries for this, try:

    https://github.com/43081j/id3
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 2, 2017 2:18 PM

All replies

  • User1168443798 posted

    Hi smd_yasin,

    >> i want to return Id3 data initial (first)chunk data uploaded is there any ways to return id3 data on first chunk downloaded.

    For your current code, I am afraid it would not return until the file uploads complete.

    For a workaround, I would suggest you try split the upload process into two parts. First part is inserting id3 tags with HttpPost, and second is updating ID3 information by file content with Httpput.

    Best Regards,

    Edward

    Monday, October 2, 2017 5:41 AM
  • User-474980206 posted
    As you code require html file I/o, why not just read the I’d tags with JavaScript. Then now upload is required to parse. There are a lot of libraries for this, try:

    https://github.com/43081j/id3
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 2, 2017 2:18 PM