-1

The file that is created by System.IO.File.Create cannot download like the file created by HttpPostedFileBase.SaveAs.

My code:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult SaveasMP4(HttpPostedFileBase file)
    {
        try
        {
            if (file != null && file.ContentLength > 0)
            {
                string extension = Path.GetExtension(file.FileName);
                var path = Path.Combine(Server.MapPath("~/Data/"), file.FileName);
                file.SaveAs(path);
                if (!extension.Equals(".mp4"))
                {
                    var ffMpeg = new NReco.VideoConverter.FFMpegConverter();
                    string tempFileName = path.Substring(0, path.LastIndexOf('.')) + ".mp4";
                    using (FileStream fileStream = System.IO.File.Create(Path.Combine(Server.MapPath("~/Data/"), tempFileName)))
                    {
                        ffMpeg.ConvertMedia(path, fileStream, Format.mp4);
                    }
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }
                }
            }
        }
        catch (Exception e)
        {
            ModelState.AddModelError("", e.Message);
            return View();
        }
        return RedirectToAction("Index");
    }

if the file is mp4 can be download by using file.SaveAs(path). But if the file is in other format it convert to mp4 successfully by using NReco.VideoConverter and cannot download because it create by System.IO.File.Create(tempFileName) . Just i can download by using ftp.
Is the problem in file access permission? If it is what should I do in file access permission?

hussein_moh
  • 27
  • 2
  • 11
  • What are `tempFileName` and `filess`? – Dave Zych Oct 13 '15 at 15:20
  • 2
    Well what happens at the moment? Is there an exception? Why are you not using `fileStream`? – Jon Skeet Oct 13 '15 at 15:20
  • I already used FileStream but cannot work. and when i try to download it show there is no file. Actually the file in the serve and i can download by ftp. – hussein_moh Oct 13 '15 at 15:28
  • You haven't shown us the mvc action that streams that file result to the user. Can you post that code please? – Slicksim Oct 14 '15 at 07:31
  • Thanks Slicksim. I post the code. – hussein_moh Oct 14 '15 at 08:55
  • You must add write permissions for folder "~/Data/" for iis worker process user – IgorL Oct 14 '15 at 08:57
  • I add the permissions for folder "~/Data/" that way i can download the file if created by file.SaveAs(path). – hussein_moh Oct 14 '15 at 08:59
  • 1
    What is the actual exception thrown? No point in adding permissions for an invalid path error. The exception message would help but the full exception text is even better as it includes the call stack and any inner exceptions (ie call e.ToString()) – Panagiotis Kanavos Oct 14 '15 at 09:10
  • also you can use Path.GetFileNameWithoutExtension instead of substring – IgorL Oct 14 '15 at 09:19
  • Panagiotis Kanavos. when i work locally it work very well. But when i work in remote host the file cannot download.And it show network error or file not found. – hussein_moh Oct 14 '15 at 10:38

1 Answers1

0

Actually the problem is related to file permissions policy that belong to the server. If the file is made by internal process like FFMpeg DLL (C++ library) the server block the IIS user from download the file. So I contact with the server team and they give permissions to IIS user to download the file that made by FFMpeg DLL.

hussein_moh
  • 27
  • 2
  • 11