c++ - How to add correct Content-Type when uploading file with Wininet? (HTTP PUT) -
i uploading file wininet sharepoint server http put. however, when specify file name .xlsx
extension, got on sharepoint server file says it's named .xlsx
, when downloading it, gets .zip
extension. also, on sharepoint, file not little excel icon next it, more generic icon. have tried every combination of setting content-type ("mime-type") httpaddrequestheaders , @ httpsendrequest come with.
the code below uploads file, sharepoint gets content-type wrong:
static int upload_file_to_sharepoint(lpcstr filename, lpcstr server, lpcstr location) { hinternet hintrn = internetopena( "magic", internet_open_type_preconfig_with_no_autoproxy, null, null, 0 ); if (!hintrn) return printf("no internet connection: %li.\n", getlasterror()); hinternet hconn = internetconnecta( hintrn, server, internet_default_https_port, null, null, internet_service_http, 0, null ); if (!hconn) return printf("connection update server failed: %li.\n", getlasterror()); dword dwopenrequestflags = internet_flag_keep_connection | internet_flag_no_cookies | internet_flag_no_cache_write | internet_flag_no_ui | internet_flag_reload; pcstr rgpszaccepttypes[] = { "text/*", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", null }; hinternet hreq = httpopenrequesta( hconn, "put", location, "http/1.1", null, rgpszaccepttypes, dwopenrequestflags, null ); handle hfile = createfilea( filename, generic_read, file_share_read, null, open_existing, file_attribute_normal, null ); if (null == hfile) { exitprocess(1); } handle hmap = createfilemapping(hfile, null, page_readonly, 0, 0, null); if (null == hmap) { exitprocess(1); } lpvoid lpvfile = mapviewoffile(hmap, file_map_read, 0, 0, 0); dword dwfilesize = getfilesize(hfile, null); char mimetype[1024]; sprintf( mimetype, "content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" //"content-type: application/vnd.ms-excel\r\n" ); if (!httpaddrequestheadersa(hreq, mimetype, -1, http_addreq_flag_replace)) { printf("failed adding mime header\n"); } if (!httpsendrequesta( hreq, null,// "content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 0, //-1, lpvfile, dwfilesize)) { printf("httpsendrequest failed: %li.\n", getlasterror()); } unmapviewoffile(lpvfile); closehandle(hmap); closehandle(hfile); printf("uploaded file http://%s%s\n", server, location); return 0; }
i captured headers fiddler, got this:
put http://x.com/censored/a.xslx http/1.1 accept: text/*, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet content-disposition: attachment; filename="a.xlsx" content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet user-agent: magic host: censored content-length: 772303 connection: keep-alive pragma: no-cache http/1.1 200 ok cache-control: private,max-age=0 content-length: 0 expires: tue, 15 sep 2015 08:43:16 gmt last-modified: wed, 30 sep 2015 08:43:16 gmt etag: "{0dc262d0-83ae-489a-90cd-eb23b284a3b3},14" server: microsoft-iis/7.5 sprequestguid: 6aba173e-41db-4b14-b7df-7714c54db282 x-sharepointhealthscore: 0 resourcetag: rt:0dc262d0-83ae-489a-90cd-eb23b284a3b3@00000000014 public-extension: http://schemas.microsoft.com/repl-2 x-powered-by: asp.net microsoftsharepointteamservices: 14.0.0.7145 x-ms-invokeapp: 1; requirereadonly servername: xcensored date: wed, 30 sep 2015 08:43:16 gmt
so, helps if upload file .xlsx
instead of .xslx
...
the code works , looks this, no special headers necessary, consistent other examples have found on web:
static int upload_file_to_sharepoint(lpcstr filename, lpcstr server, lpcstr location) { hinternet hintrn = internetopena( "magic", internet_open_type_preconfig_with_no_autoproxy, null, null, 0 ); if (!hintrn) return printf("no internet connection: %li.\n", getlasterror()); hinternet hconn = internetconnecta( hintrn, server, internet_default_https_port, null, null, internet_service_http, 0, null ); if (!hconn) return printf("connection update server failed: %li.\n", getlasterror()); dword dwopenrequestflags = internet_flag_keep_connection | internet_flag_no_cookies | internet_flag_no_cache_write | internet_flag_no_ui | internet_flag_reload; hinternet hreq = httpopenrequesta( hconn, "put", location, "http/1.1", null, null, dwopenrequestflags, null ); handle hfile = createfilea( filename, generic_read, file_share_read, null, open_existing, file_attribute_normal, null ); if (null == hfile) { exitprocess(1); } handle hmap = createfilemapping(hfile, null, page_readonly, 0, 0, null); if (null == hmap) { exitprocess(1); } lpvoid lpvfile = mapviewoffile(hmap, file_map_read, 0, 0, 0); dword dwfilesize = getfilesize(hfile, null); if (!httpsendrequesta( hreq, null, 0, lpvfile, dwfilesize)) { printf("httpsendrequest failed: %li.\n", getlasterror()); } unmapviewoffile(lpvfile); closehandle(hmap); closehandle(hfile); printf("uploaded file http://%s%s\n", server, location); return 0; }
Comments
Post a Comment