1

这是我的 API 方法,它将 .caf 文件上传到服务器,转换为 .mp3 并删除 .caf 文件。但问题是原始文件和转换后的文件都被删除,而不仅仅是原始文件。

[HttpPost]
    [ActionName("UploadCompetitionEntry")]
    public async Task<HttpResponseMessage> UploadCompetitionEntry([FromUri]string folderName)
    {
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
        }

        string path = System.Web.Hosting.HostingEnvironment.MapPath("~/" + folderName);

        MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(path);
        var task = Request.Content.ReadAsMultipartAsync(provider);

        // Log exceptions
        await task.ContinueWith(t =>
        {
            if (t.IsFaulted)
            {
                // Log t.Exception
            }
        });           

            var bodyPart2 = provider.FileData.Where(p => p.Headers.ContentDisposition.Name.Replace("\"", string.Empty) == folderName).FirstOrDefault();

            if (bodyPart2 != null)
            {
                string savedFile2 = bodyPart2.LocalFileName;
                string originalFile2 = bodyPart2.Headers.ContentDisposition.FileName.Replace("\"", string.Empty);
                string uniqueFilename = string.Format(@"{0}", Guid.NewGuid());
                string newFile2 = uniqueFilename + Path.GetExtension(originalFile2);

                // Copy file and rename with new file name and correct extension
                FileInfo file2 = new FileInfo(savedFile2);
                file2.CopyTo(Path.Combine(path, newFile2), true);
                file2.Delete();


                if (folderName == "music")
                {
                    //converting .caf to .mp3 and creating a new .mp3 file
                    MediaFuncs.ConvertToMp3(Path.Combine(path, newFile2), uniqueFilename);

                    //deleting the .caf file
                    FileInfo file3 = new FileInfo(Path.Combine(path, newFile2));
                    file3.Delete();
                }
            }         


        return Request.CreateResponse(HttpStatusCode.OK, new ResponseMessage<Object> { success = true, message = "Media Uploaded" });

    }   

这是我的转换器方法

 public static void ConvertToMp3(string filename, string uniqueFilename)
    {
        string musicPath = System.Web.Hosting.HostingEnvironment.MapPath("~/" + "music");

        System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();

        info.FileName = System.Web.Hosting.HostingEnvironment.MapPath("~/" + "ffmpeg.exe");
        info.Arguments = " -i " + filename + " " + Path.Combine(musicPath, uniqueFilename + ".mp3");

        System.Diagnostics.Process p1 = System.Diagnostics.Process.Start(info);
    }

难道我做错了什么?如果有人知道删除原始文件的任何其他方法,请告诉我,我整天都在为此烦恼:(

编辑:我放了一个断点并检查了。我想在转换完成之前删除 .caf 文件。因为当我在调用转换器方法之后和删除文件之前放置断点时,.mp3 没有被删除。

那么现在我如何停止删除直到转换完成?

4

1 回答 1

2

在删除源文件之前,您需要等待转换完成。

为此,您可以简单地调用:

p1.WaitForExit();

在结束时ConvertToMp3

请参阅WaitForExitMSDN。

于 2012-12-12T15:09:09.027 回答