2017-11-28 1 views
-3

Ich habe gesucht, um einen jQuery (Ajax) -Code zu finden, der Bild von einem Formular (natürlich hat das Formular auch andere Eingaben) zu Aktion als zweiten Parameter.Wie Bild von einem Formular in Aktion als zweiten Parameter zu buchen?

Ich möchte auch, dass es Daten nacheinander initialisiert.Es gibt irgendwelche Lösungen, um mein Problem zu lösen? Bitte helfen Sie mir.

var formDatas = new FormData(); 
var uploadFile= $("#fileInput").get(0).files; 

if(uploadFile.lenght > 0){ 
    formDatas.append("uploadFile",uploadFile[0]); 
} 

$.ajax({ 
     type: 'POST', 
     url: '/Your_Controller_Name/Your_Action_Name', 
     data:formDatas, 
     dataType: "json", 
     success: function (data) { 
      alert("Success!"); 
     } 
    }); 

In der Steuerung

+0

Sie verwenden können ['formData'] (https://developer.mozilla.org/en-US/docs/Web/API/FormData) um anzugebende Werte anzuhängen. Sie können auch [eine Datei dazu hinzufügen] (https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects) –

Antwort

0

ein Bild hochladen, können Sie den <input>-Tag als

<input id="fileInput" type="file" /> 

In Ihrer Skriptdatei den folgenden Code verwenden folgt können Sie Bild wie diese

erhalten
if (System.Web.HttpContext.Current.Request.Files.AllKeys.Any()) 
{ 
    var picture = System.Web.HttpContext.Current.Request.Files["uploadFile"]; 
} 
+0

das ist gut, aber es nur die Datei als separate Datei veröffentlichen . Aber ich habe Idee von Ihrer Antwort gefunden, um mein Problem zu lösen. Danke – bami

+0

Im Controller können Sie auch andere Werte mit HttpContext.Current.Request.Params ["Your_Model_Property_Name"]; –

0

Endlich habe ich dieses Problem gelöst.

i eine normale Form geschaffen hatte, die einige Eigenschaften und Bilddatei unter ihnen hatte:

 <h1 class="block-header">افزودن گروه</h1> 
     <div class="separator"></div> 


     <div class="block-standart"> 

      <div class="form inline"> 
       <fieldset> 
        <legend>Product</legend> 





        <div class="group"> 
         @Html.LabelFor(model => model.Product.Name, new { @class = "label" }) 
         <div class="controls"> 
          @Html.TextBoxFor(model => model.Product.Name, new { @class = "text", id = "Name" }) 
          @Html.ValidationMessageFor(model => model.Product.Name) 
         </div> 
        </div> 







        <div class="group"> 
         @Html.LabelFor(model => model.Product.Description, new { @class = "label" }) 
         <div class="controls"> 

          @Html.TextBoxFor(model => model.Product.Description, new { @class = "text", id = "description" }) 
          @Html.ValidationMessageFor(model => model.Product.Description) 
         </div> 
        </div> 



        <div class="group"> 
         @Html.LabelFor(model => model.Product.Keyword, new { @class = "label" }) 
         <div class="controls"> 
          <div id="t"> 
           @Html.TextBoxFor(model => model.Product.Keyword, new { @class = "text", id = "Keyword" }) 
           @*<input type="text" name="tags" placeholder="Tags" class="tm-input" />*@ 
           @*<input type="text" data-toggle="tags" id="s" />*@ 

          </div> 
         </div> 

        </div> 
        <div class="group"> 
         @Html.LabelFor(model => model.Product.Like, new { @class = "label" }) 
         <div class="controls"> 

          @Html.TextBoxFor(model => model.Product.Like, new { @class = "text" }) 
          @Html.ValidationMessageFor(model => model.Product.Like) 
         </div> 
        </div> 



        <div class="group"> 
         @Html.LabelFor(model => model.Product.DisLike, new { @class = "label" }) 
         <div class="controls"> 

          @Html.TextBoxFor(model => model.Product.DisLike, new { @class = "text" }) 
          @Html.ValidationMessageFor(model => model.Product.DisLike) 
         </div> 
        </div> 



        <div class="group"> 
         @Html.LabelFor(model => model.Product.URL, new { @class = "label" }) 
         <div class="controls"> 

          @Html.TextBoxFor(model => model.Product.URL, new { @class = "text" }) 
          @Html.ValidationMessageFor(model => model.Product.URL) 
         </div> 
        </div> 

        <div class="group"> 
         @Html.LabelFor(model => model.Product.Enable, new { @class = "label" }) 
         <div class="controls"> 

          @Html.CheckBoxFor(model => model.Product.Enable, new { @class = "text" }) 
          @Html.ValidationMessageFor(model => model.Product.Enable) 
         </div> 
        </div> 







        <div class="group"> 
         @Html.LabelFor(model => model.Product.Tags, new { @class = "label" }) 
         <div class="controls"> 

          @Html.TextBoxFor(model => model.Product.Tags, new { @class = "text", id = "tags" }) 
         </div> 
        </div> 



        <div class="group"> 
         @Html.LabelFor(model => model.Product.Image, new { @class = "label" }) 
         <div class="controls"> 
          <input type="file" id="UploadImage" name="UploadImage" /> 
         </div> 
        </div> 



        <div class="group"> 
         @Html.LabelFor(model => model.Product.Summery, new { @class = "label" }) 
         <div class="controls"> 

          @Html.TextAreaFor(model => model.Product.Summery, new { @class = "text", id = "txtSum" }) 
          @Html.ValidationMessageFor(model => model.Product.Summery) 
         </div> 
        </div> 



        <div class="group"> 
         @Html.LabelFor(model => model.Product.Price, new { @class = "label" }) 
         <div class="controls"> 

          @Html.TextBoxFor(model => model.Product.Price, new { @class = "text", id = "price" }) 
          @Html.ValidationMessageFor(model => model.Product.Price) 
          <input type="text" id="gi" name="Product.GroupId" /> 
         </div> 
        </div> 

        <p> 
         <input type="submit" value="Create" id="btnSabt" onclick="ExceptImage();" /> 
        </p> 
        <textarea id="e">jg</textarea> 
       </fieldset> 
      </div> 
     </div> 
    </form> 

und mein Controller war:

  public ActionResult AddProduct(Product product, HttpPostedFileBase 
    UploadImage) 
     { 
      ProductRepository blProduct = new ProductRepository(); 
      if (ModelState.IsValid) 
      { 
       product.Image = UploadImage.FileName; 
      string path = Server.MapPath("~") + "Files\\UploadImages\\" + 
    UploadImage.FileName; 
      UploadImage.InputStream.ResizeImageByWidth(500, path, 
Utilty.ImageComperssion.Normal); 

      if (blProduct.Add(product)) 
      { 
       return MessageBox.Show("محصول با موفقیت ثبت شد", MessageType.Success); 
      } 
      else 
      { 
       System.IO.File.Delete(path); 
       return MessageBox.Show("محصول ثبت نشد", MessageType.Error); 
      } 
     } 
     else 
     { 
      //مقدار ورودی اشتباه 
      return MessageBox.Show(ModelState.GetErrors(), MessageType.Warning); 
     } 
    } 

I posted Daten in 2 Schritten:

1-i hat alle Daten außer der Datei veröffentlicht.

2-i hat die Datei als separate Datei veröffentlicht.

In Schritt 1 habe ich Onclilck-Ereignis verwendet und eine Funktion namens ExceptImage verwendet.

in ExceptImage Gebraucht i ajax alle Daten mit Ausnahme der Bilddatei zu schreiben:

function ExceptImage(e) { 

     e.preventDefault(); 
     $.ajax({ 
      url: "/Admin/AddProduct", 
      type: "post", 
      data: { 
       Name: $("#Name").val(), 
       Description: $("#description").val(), 
       Keyword: $("#Keyword").val(), 
       Like: $("#Product_Like").val(), 
       DisLike: $("#Product_DisLike").val(), 
       URL: $("#Product_URL").val(), 
       Enable: $("#Product_Enable").val(), 
       Summery: $("#txtSum").val(), 
       Tags: $("#Product_Tags").val(), 
       GroupId: $("#gi").val() 
      } 
     }) 
    } 

in Schritt 2 i Form Veranstaltung einreichen verwendet (jQuery) und die Datei geschrieben:

$("#myUploadForm").submit(function (e) { 

      e.preventDefault(); 
      var fd = new FormData(this) 
      $.ajax({ 
       url: '/Admin/AddProduct', 
       type: 'POST', 
       data: fd, 
       contentType: false, 
       cache: false, 
       processData:false 

      }) 
     }) 
Verwandte Themen