2017-03-13 15 views
-1

ASP.NET CVアプリケーションで画像をアップロードしたいと思います。ファイルアップロードではありません。リンクをクリックして写真をアップロードしたいのですが。どうしたらいいですか? Thnaks :)ASP.NET CVアップロード画像

profile picture in application

答えて

-1

私はあなたが特別のFileUploadを使用していないために尋ねたが、念のために見ました。

あなたは、ビューポートのうち、FUコントロールを移動し、LinkBut​​tonコントロールを追加し、

<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> 

と分離コード内に似OnClientClickでJSを持つことができます。

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."; 
        } 
       } 
      } 
     } 
+0

私はプロジェクトの例を教えていただけますか? –

+0

答えを –

+0

私は –

-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は写真が保存されるテーブル名です。

+0

のためのdownvoteを知って興味深いだろうこのコードは、MVCコードです。私はASP.NETをしたいです。 –

+0

さて、とにかく.csコードを使用することができます。ちょうどその方法を見直してから、自分のコードに適応してください。 – ilkemerol

関連する問題