通过web api 2.0将图像从虚拟目录上传到服务器

时间:2022-09-03 21:21:10

I have web api using which I am able to upload image to server when client is mobile app but not web client is asp.net web from.

我有web api使用,当客户端是移动应用程序时,我能够将图像上传到服务器但不是web客户端是asp.net web。

I am able to save image to virtual folder but then not able to upload via web api which stores both web/mobile uploaded images.

我能够将图像保存到虚拟文件夹,但无法通过web api上传,该网页存储了Web /移动上传的图像。

I am sure I am making mistake in writing client side code in asp.net or not able to handle proper postback of fileupload control please guide me where I am wrong.

我确信我在asp.net中编写客户端代码时犯了错误,或者无法处理文件上载控件的正确回发,请指导我错误的地方。

here is API - "*.com/a/35449422/8875271"; which I am using I just want client code/sample for asp.net web form using image upload control I have tired this but this didn't seems working please help I am stuck.

这是API - “*.com/a/35449422/8875271”;我正在使用我只是想要使用图像上传控件的asp.net web表单的客户端代码/示例我已经厌倦了这个但是这似乎没有用,请帮助我被卡住了。

API working file on mobile as well as on postman.

移动设备上的API工作文件以及邮递员。

Design

设计

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

code

 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");
    }
}

error

错误

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

1 个解决方案

#1


0  

This change made it work

这一改变使其成功

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

#1


0  

This change made it work

这一改变使其成功

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