簡體   English   中英

通過Web API 2.0將圖像從虛擬目錄上傳到服務器

[英]upload image from virtual directory to server via web api 2.0

我有Web api,當客戶端是移動應用程序但Web客戶端不是asp.net Web時,我可以使用它向服務器上傳圖像。

我能夠將圖片保存到虛擬文件夾,但無法通過同時存儲Web /移動設備上傳圖片的Web API上傳。

我確定在asp.net中編寫客戶端代碼時出錯,或者無法處理正確的文件上傳控制回發,請在錯誤的地方指導我。

這是API-“ stackoverflow.com/a/35449422/8875271 ”; 我正在使用的我只希望使用圖像上傳控件的asp.net Web表單的客戶端代碼/示例,我對此感到厭倦,但這似乎無法正常工作,請幫助我。

移動和郵遞員上的API工作文件。

設計

    <asp:UpdatePanel ID="filepanel" runat="server">
    <ContentTemplate>
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:LinkButton ID="LinkButton1" runat="server" Text="Attach" OnClick="LinkButton1_Click"></asp:LinkButton>
     </ContentTemplate>
     <Triggers>
      <asp:PostBackTrigger ControlID="LinkButton1"  />
  </Triggers>
     </asp:UpdatePanel>

 protected void LinkButton1_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        System.Diagnostics.Debug.WriteLine(" uploaded");
        APIs api = new APIs();
        using (HttpClient client = new HttpClient())
        {
            try
            {

                string filename = FileUpload1.PostedFile.FileName;
                Session["FileUpload1"] = filename;

                FileUpload1.SaveAs(Server.MapPath("images\\" + FileUpload1.PostedFile.FileName));

                using (var fileStream = File.Open(Server.MapPath("images\\" + Session["FileUpload1"].toString() ), FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
                {
                    System.Diagnostics.Debug.WriteLine("image file opened");
                    var content = new MultipartFormDataContent();
                    if(fileStream!=null)
                    content.Add(new StreamContent(fileStream));
                    var response = client.PostAsync(api.pictureUpload, content).Result; -------Line 174
                    if (response.IsSuccessStatusCode)
                    {
                        System.Diagnostics.Debug.WriteLine("File Uploaded Successfully");
                    }
 else
 {
  System.Diagnostics.Debug.WriteLine("File Uploaded 
 UnSuccessfully"+response.RequestMessage);
 }
                }

            }
            catch (IOException iex)
            {
                System.Diagnostics.Debug.WriteLine("io exception:" + iex.ToString());
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("image path:" + ex.ToString());
            }
        }
    }
    else
    {
        System.Diagnostics.Debug.WriteLine("no file");
    }
}

錯誤

    System.AggregateException: One or more errors occurred. ---> 
    System.NotSupportedException: Stream does not support reading.
    at System.IO.Stream.BeginReadInternal(Byte[] buffer, Int32 offset, Int32 
    count, AsyncCallback callback, Object state, Boolean serializeAsynchronously)
    at System.IO.FileStream.BeginRead(Byte[] array, Int32 offset, Int32 numBytes, 
    AsyncCallback userCallback, Object stateObject)
   at System.Net.Http.StreamToStreamCopy.StartRead()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at TransferToMIR.LinkButton1_Click(Object sender, EventArgs e) in path\TransferToMIR.aspx.cs:line 174
---> (Inner Exception #0) System.NotSupportedException: Stream does not support reading.
   at System.IO.Stream.BeginReadInternal(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state, Boolean serializeAsynchronously)
   at System.IO.FileStream.BeginRead(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback userCallback, Object stateObject)
   at System.Net.Http.StreamToStreamCopy.StartRead()<---

這項更改使其有效

using (var fileStream = File.OpenRead(Server.MapPath("images\\" + Session["Filename"].toString())))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM