File.Delete删除原始文件和转换文件

时间:2012-12-12 14:41:37

标签: c# asp.net file file-io asp.net-web-api

这是我的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没有被删除。

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

1 个答案:

答案 0 :(得分:2)

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

为此,您只需致电:

p1.WaitForExit();

ConvertToMp3结尾。

请参阅MSDN上的WaitForExit