Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 166457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:06:04+00:00 2026-05-11T12:06:04+00:00

1 namespace Uploader 2 { 3 using System; 4 using System.IO; 5 using System.ServiceModel;

  • 0
1 namespace Uploader   2 {   3     using System;   4     using System.IO;   5     using System.ServiceModel;   6     using System.ServiceModel.Description;   7     using System.ServiceModel.Web;   8     using System.Drawing;   9     using System.Drawing.Imaging;   10     using System.Net;   11     using System.Xml;   12    13     [ServiceContract(Namespace = 'http://Uploader')]   14     public interface IUploaderService   15     {   16         [OperationContract, WebInvoke(Method = 'POST',UriTemplate = 'File/{fileName}')]   17         bool UploadFile(string fileName, Stream fileContents);   18     }   19    20     [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]   21     public class UploaderService : IUploaderService   22     {   23         public bool UploadFile(string fileName, Stream fileContents)   24         {   25             return true;   26         }   27     }   28    29     class Program   30     {   31         static void Main()   32         {   33             var host = new    34                 ServiceHost(typeof (UploaderService),    35                 new Uri('http://localhost:8080/Uploader'));   36             host.AddServiceEndpoint('Uploader.IUploaderService',    37                 new WebHttpBinding(), '').Behaviors.Add(new WebHttpBehavior());   38             try   39             {   40                 host.Open();   41                 Console.WriteLine(host.BaseAddresses[0].AbsoluteUri + ' running.');   42                 Console.WriteLine();   43                 var uri = 'http://localhost:8080/Uploader/file.jpg';   44                 var req = WebRequest.Create(uri) as HttpWebRequest;   45                 if (req != null)   46                 {   47                     req.Method = 'POST';   48                     req.ContentType = 'image/jpeg';   49                     var reqStream = req.GetRequestStream();   50                    51                     var imageStream = new MemoryStream();   52                     using (var i = Image.FromFile(@'c:\photo.jpg'))    53                         i.Save(imageStream, ImageFormat.Jpeg);   54                        55                     var imageArray = imageStream.ToArray();   56                     reqStream.Write(imageArray, 0, imageArray.Length);   57                     reqStream.Close();   58                     var resp = (HttpWebResponse)req.GetResponse();   59                     var r = new XmlTextReader(resp.GetResponseStream());   60                     if (r.Read())   61                     {   62                         Console.WriteLine(r.ReadString());       63                     }   64                 }   65                 Console.WriteLine('Press <ENTER> to quit.');   66                 Console.ReadLine();   67             }   68             catch (Exception ex)   69             {   70                 Console.WriteLine(ex.Message);   71                 Console.ReadKey();   72             }   73             finally   74             {   75                 if (host.State == CommunicationState.Faulted)   76                     host.Abort();   77                 else   78                     host.Close();   79             }   80         }   81     }   82 }   83    84    

Hi, hope you can help….

I am creating a simple app(maybe webpage) that will have a simple UI and will upload files from an external device, the app/webpage will be started via autorun.inf when the user plugs a device into there PC. The webservice will perform the complex job of linking the file to the management system etc. This will enable the IT illiterate users that can’t use file explore to submit files to the the management system…!

The problem I have is that my RESTful serivce is giving me a 400 error when the content type is a image/jpeg.. It works fine for text/plain or text/xml (see Blog Post)

Thanks J

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:06:05+00:00Added an answer on May 11, 2026 at 12:06 pm

    You can try to override any content-type and upload all files as application/octet-stream, or text/plain using an IOperationBehavior.

    public class WebContentTypeAttribute : Attribute, IOperationBehavior, IDispatchMessageFormatter {     private IDispatchMessageFormatter innerFormatter;     public string ContentTypeOverride { get; set; }      public WebContentTypeAttribute(string contentTypeOverride)     {         this.ContentTypeOverride = contentTypeOverride;     }       // IOperationBehavior     public void Validate(OperationDescription operationDescription)     {      }      public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)     {         innerFormatter = dispatchOperation.Formatter;         dispatchOperation.Formatter = this;     }      public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)     {      }      public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)     {      }      // IDispatchMessageFormatter     public void DeserializeRequest(Message message, object[] parameters)     {         if (message == null)             return;          if (string.IsNullOrEmpty(ContentTypeOverride))             return;          var httpRequest = (HttpRequestMessageProperty)message.Properties[HttpRequestMessageProperty.Name];         httpRequest.Headers['Content-Type'] = ContentTypeOverride;     }      public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)     {         return innerFormatter.SerializeReply(messageVersion, parameters, result);     } } 

    And you would have to modify your Service contract to look like this one

    [OperationContract] [WebInvoke(Method = 'POST',UriTemplate = 'File/{fileName}')] [WebContentType('application/octet-stream')] bool UploadFile(string fileName, Stream fileContents);   

    Although, if you are uploading from a webpage, wouldn’t the data be posted in a multipart/form-data format?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 117k
  • Answers 117k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It looks like you're not using the click event properly.… May 11, 2026 at 10:49 pm
  • Editorial Team
    Editorial Team added an answer Here is more reliable way (as I said, Transaction.Current can… May 11, 2026 at 10:49 pm
  • Editorial Team
    Editorial Team added an answer I think this is the basic idea. You can fill… May 11, 2026 at 10:49 pm

Related Questions

I'm writing an add-in for ReSharper 4. For this, I needed to reference several
I'm trying to access a WebService using nuSOAP (because I'm bound to PHP4 here)
Ok, so I'm new to VB.NET and trying to write a program that prompts
I'm having trouble with loading and compiling a new version of existing classes. At

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.