ASP.NET CV上传图片

时间:2017-03-13 05:42:39

标签: asp.net profile

我想在ASP.NET CV应用程序中上传图像。它不是文件上传。我想通过点击链接上传图片。我该怎么做? Thnaks :)

profile picture in application

2 个答案:

答案 0 :(得分:-1)

我确实看到你明确要求不使用FileUpload,但以防万一。

您可以将FU控件移出视口,并添加一个LinkBut​​ton,它将在OnClientClick中具有类似于

的JS
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <script>
        function clickFile() {
            afl = document.getElementById("<%=FU.ClientID%>");
            afl.click();
            __doPostBack('__Page', 'Argument');
        }
    </script>
    <asp:FileUpload runat="server" ID="FU" style="position:absolute;left:-4000px;" />
    <asp:LinkButton ID="asd" runat="server" OnClientClick="javscript:{clickFile();}">click</asp:LinkButton>
    <asp:Label runat="server" ID="Label1"></asp:Label>
</asp:Content>

和代码隐藏:

protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                Boolean fileOK = false;
                if (FU.HasFile)
                {

                    fileOK = true;

                }

                if (fileOK)
                {
                    try
                    {
                        FU.PostedFile.SaveAs(@"C:\temp\" + FU.FileName);
                        Label1.Text = "File uploaded!";
                    }
                    catch (Exception ex)
                    {
                        Label1.Text = "File could not be uploaded.";
                    }
                }
            }
        }

答案 1 :(得分:-2)

我做了类似的事,

CSHTML;

<div class="form-group animated fadeInLeft">
  <label class="col-md-2 control-label">Select File : </label>
    <div class="col-md-10">
        @Html.TextBox("file", null, new { type="file", @class = "form-control", accept="image/x-pngi image/gif, image/jpeg" })
        @Html.ValidationMessage("FileErrorMessage")
      </div>
</div>

HomeController或SomeController.cs;

public ActionResult Add(Contact c, HttpPostedFileBase file){
   if (file != null)
        {
            if (file.ContentLength > (512 * 100))
            {
                ModelState.AddModelError("FileErrorMessage", "File size must be within 512KB");
            }
            string[] allowedType = new string[] { "image/png", "image/gif", "image/jpeg", "image/jpg" };
            bool isFileTypeValid = false;
            foreach (var i in allowedType)
            {
                if(file.ContentType == i.ToString())
                {
                    isFileTypeValid = true;
                    break;
                }
            }
            if (!isFileTypeValid)
            {
                ModelState.AddModelError("FileErrorMessage", "Only .png,.gif and jpg");
            }
        }

        if (ModelState.IsValid)
        {
            if (file != null)
            {
                string savePath = Server.MapPath("~/Images");
                string fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);
                file.SaveAs(Path.Combine(savePath, fileName));
                c.ImagePath = fileName;
            }
            using (MyContactBookEntities dc = new MyContactBookEntities())
            {
                dc.Contacts.Add(c);
                dc.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        else
        {
            return View(c);
        }
    }

MyContactBookEntities是我的数据库连接,Contacts是一个表名,用于保存图片。