【问题标题】:ASP.NET CV Upload imageASP.NET CV 上传图片
【发布时间】:2017-03-13 05:42:39
【问题描述】:

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

【问题讨论】:

    标签: asp.net profile


    【解决方案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.";
                        }
                    }
                }
            }
    

    【讨论】:

    • 你能给我一个示例项目吗?
    • 想知道我对什么投了反对票会很有趣
    【解决方案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 是要保存图片的表名。

    【讨论】:

    • 好吧,反正你可以使用.cs代码,只要查看那个方法有一个想法,然后适应你自己的代码。
    猜你喜欢
    • 2011-05-22
    • 1970-01-01
    • 2018-04-17
    • 2018-05-02
    • 2023-03-07
    • 2016-06-24
    • 2022-12-17
    • 2014-07-27
    • 2018-04-21
    相关资源
    最近更新 更多