0

我希望有人可以在下面修改我的代码,以准确地告诉我如何让它做我想做的事。

我有一个发布到以下操作的 HTML 表单:

public ActionResult Create(string EntryTitle, string EntryVideo, HttpPostedFileBase ImageFile, string EntryDesc)
    {
        if (Session["User"] != null)
        {
            User currentUser = (User)Session["User"];

            string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(ImageFile.FileName)); 
            ImageFile.SaveAs(savedFileName);

            Entry newEntry = new Entry();

            newEntry.Title = EntryTitle;
            newEntry.EmbedURL = EntryVideo;
            newEntry.Description = EntryDesc;
            newEntry.ImagePath = savedFileName;
            newEntry.UserId = currentUser.UserId;

            db.Entries.Add(newEntry);
            db.SaveChanges();


        }

        return RedirectToAction("MyPage", "User");
    }

这会将图像保存到根解决方案目录(或尝试这样做,没有权限并引发异常)。

我想做的是以下几点:

1.) 验证文件大小是否低于某个最大值,现在假设为 500kb

2.)假设文件大小没问题,将其保存到以下目录

mywebsite.com/uploads/<userid>/<entryid>/entry-image.<jpg/png/gif>

我不确定如何重命名文件,因为我想接受不同的文件扩展名(.jpeg、.jpg、.png、.gif)。或者不确定如何将其放入上述正确目录。或者如何验证文件大小,因为如果用户使用的是 IE,显然你只能使用 javascript 来做到这一点。

4

2 回答 2

1

1.验证文件大小是否低于某个最大值,现在假设为 500kb

您可以使用HttpPostFileBase.ContentLength属性来获取文件的大小(以字节为单位)。

if (ImageFile.ContentLength > 1024 * 500) // 1024 bytes * 500 == 500kb
{
    // The file is too big.
}

2.假设文件大小没问题,保存到以下目录

string savedFileName = Server.MapPath(string.Format("~/uploads/{0}/{1}/entry-image{2}",
    currentUser.UserId,
    newEntry.EntryId,
    Path.GetExtension(ImageFile.FileName)));

我看到的唯一问题是您的 Entry.EntryId 看起来可能是在数据库中生成的,因此在生成之前您将无法将其用作保存路径的一部分。

于 2012-04-05T23:17:30.533 回答
0

希望这有助于或至少为您指明正确的方向

    if (ImageFile.ContentLength < 1024 * 500)
            {
                Entry newEntry = new Entry();

                newEntry.Title = EntryTitle;
                newEntry.EmbedURL = EntryVideo;
                newEntry.Description = EntryDesc;
                newEntry.UserId = currentUser.UserId;

                db.Entries.Add(newEntry);
                db.SaveChanges();  //this helps generate an entry id

                string uploadsDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "uploads");
                string userDir = Path.Combine(uploadsDir, <userid>);
                string entryDir = Path.Combine(userDir, newEntry.EntryID );

                if (Directory.Exists(userDir) == false)
                    Directory.CreateDirectory(userDir);

                if (Directory.Exists(entryDir) == false)
                    Directory.CreateDirectory(entryDir);

               string savedFileName = Path.Combine(entryDir, <entry-image>);
               ImageFile.SaveAs(savedFileName);

               newEntry.ImagePath = savedFileName;  //if this doesn't work pull back out this entry and adjust the ImagePath
               db.SaveChanges();

            }

您应该授予“上传”目录的写入权限。

您还可以从 web.config 限制您的 Web 应用程序的文件大小

    <system.web>
       <httpRuntime   maxRequestLength="500"/>
于 2012-04-05T23:44:51.377 回答